Hi guys. I have this code that pulls user info and using COALESCE it pulled the default image from the user’s photos. It works great but I now want to modify it to include another table named “unlocked” where u.user_id = unlocked.id
I’m not sure how to do this as the join is already very complicated for my experience level. My attempts fail
SELECT
u.name, u.age, u.category, u.info1, u.info12, u.lastlogin, u.lastactivity
,COALESCE(u2i.url,u2i2.url) url
FROM
usertable u
LEFT OUTER
JOIN
imagetable u2i
ON
u.name = u2i.name
AND
u2i.default = 'Y'
LEFT OUTER
JOIN
imagetable u2i2
ON
u.name = u2i2.name
WHERE
(u2i.priv != 1 && u2i.status = 'active' OR u2i2.priv !=1 && u2i2.status = 'active') group by u.name DESC LIMIT 10
in your first post you said you wanted where u.user_id = unlocked.id, but your subquery uses name?
it appears that the GROUP BY clause is intended to return only one image for each user, which would be a random one, so i’ve pushed that down into a separate subquery
also, if you want to use LIMIT to do paging (i.e. x rows per page), you pretty much have to use an ORDER BY clause for reliable results
i tried making a few other changes, please let me know if you get the same error message
SELECT u.name
, u.age
, u.category
, u.info1
, u.info12
, u.lastlogin
, u.lastactivity
, COALESCE(u2i.url,u2i2.url) url
FROM usertable u
INNER
JOIN ( SELECT DISTINCT name
FROM Unlocked
WHERE id = $newcomid ) AS d
ON d.name = u.name
LEFT OUTER
JOIN imagetable u2i
ON u2i.name = u.name
AND u2i.default = 'Y'
LEFT OUTER
JOIN ( SELECT name
, MIN(url) AS min_url
FROM imagetable
GROUP
BY name ) AS u2m
ON u2m.name = u.name
LEFT OUTER
JOIN imagetable u2i2
ON u2i2.name = u.name
AND u2i1.url = u2m.min_url
WHERE (
u2i.priv <> 1 AND u2i.status = 'active'
OR u2i2.priv <> 1 AND u2i2.status = 'active'
)