I've got two tables in mySQL. coffee_users and coffee_product_registrations. Each user can have multiple product registrations. I'm trying to select users with more than 1 product registration, but I'm getting the following error:
#1054 - Unknown column 'coffee_users.uid' in 'on clause'
When executing this:
SELECT c.uid
FROM
`coffee_users` as c
JOIN (
select uid
from `coffee_users`
group by `uid`
having count(*)>1
) coffee_product_registrations
on coffee_users.uid = coffee_product_registrations.uid
Is this doable at all?
Maybe this is what you want?
SELECT c.uid
FROM coffee_users c
JOIN coffee_product_registrations cpr on c.uid = cpr.uid
GROUP BY c.uid
HAVING COUNT(cpr.id) > 1
Related
I'm trying to get the user with oldest account created using this:
SELECT a.ID
, a.username
FROM users a
JOIN
( SELECT MAX(date_created)
FROM other_info
) b
ON a.ID = b.ID;
It's returning the following; `Error Code: 1054.
Unknown column 'b.ID' in 'on clause'`
I've looked though the [mysql-error-1054] tag and haven't been able to find a solution, I'm not even sure which part of the query is wrong.
Thanks.
To find the user with the first (oldest) date_created:
SELECT u.ID, a.username, b.date_created
FROM users a
INNER JOIN other_info b
ORDER BY b.date_created
LIMIT 1
BTW, instead of a, b etc, I'd rather chose table aliases that make sense, e.g. u for users.
SELECT a.ID
, a.username
, MAX(date_created)
FROM users a
INNER JOIN other_info b
ON a.ID = b.ID;
Try This :
SELECT a.ID, a.username
FROM users a INNER JOIN
(SELECT top 1 id,date_created FROM other_info order by date_created desc) b
ON a.ID = b.ID;
I have a mySQL statement with two joins like this:
SELECT a.userID, sum(b.score) as totalScore, c.userName FROM b
Inner Join a on b.noteID = a.noteID
Inner Join user on c.userID = a.userID
Where totalScore != 0
group by a.userID order by totalScore desc
But when I run the query it can not decipher the sum that I labeled with the as
LIMIT 0, 1000 Error Code: 1054. Unknown column 'totalScore' in 'where clause' 0.18
Use a having clause instead
select a.userID, c.userName,
sum(b.score) as totalScore
FROM b
Inner Join a on b.noteID = a.noteID
Inner Join user on c.userID = a.userID
group by a.userID, c.userName
having totalScore != 0
order by totalScore desc
You can't use alias names from the select in the where clause
totalScore is a value that refers to the whole group and not just a single record, so you have to put it in the having
You should not select c.userName since it is neither aggregated nor in the group by clause. In strict mode MySQL throws an error and other DB engines will always throw an error
I get the error below:
ER_BAD_FIELD_ERROR: Unknown column 'c.id' in 'where clause':
SELECT *
FROM clients c
LEFT JOIN
(SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
WHERE client_id = c.id) AS l ON
l.client_id = c.id
I need use WHERE to group smpp_user columns for each c.id from main SELECT.
Help please? I believe it's possible.
Just remove WHERE clause in your sub query and use GROUP BY:
SELECT *
FROM clients c
LEFT JOIN (
SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
GROUP BY client_id
) AS l ON l.client_id = c.id
Basically I'm just unsure as to why this query is failing to execute:
(SELECT replies.reply_post, replies.reply_content, replies.reply_date AS d, members.username
FROM (replies) AS a
INNER JOIN members ON replies.reply_by = members.id)
UNION
(SELECT posts.post_id, posts.post_title, posts.post_date AS d, members.username
FROM (posts) as b
WHERE posts.post_set = 0
INNER JOIN members ON posts.post_by = members.id)
ORDER BY d DESC LIMIT 5
I'm getting this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'a INNER JOIN members ON replies.re' at line 2
All I'm trying to do is select the 5 most recent rows (dates) from these two tables. I've tried Join, union etc and I've seen numerous queries where people have put another query after the FROM statement and that just makes no logical sense to me as to how that works?
Am I safe to say that you can join the same table from two different but joined queries? Or am I taking completely the wrong approach, because frankly I can't seem see how this query is failing despite reading the error message.
(The two queries on there own work fine)
I think there is syntax error in your query at below part :
FROM (posts) as b
WHERE posts.post_set = 0
INNER JOIN members ON posts.post_by = members.id)
Inner join should come first before where condition. Also your join conditions are wrong. You need to apply conditions like
INNER JOIN members ON a.reply_by = members.id)
INNER JOIN members ON b.post_by = members.id)
So your query should be like this
(SELECT a.reply_post, a.reply_content, a.reply_date AS d, members.username
FROM (replies) AS a
INNER JOIN members ON a.reply_by = members.id)
UNION
(SELECT b.post_id, b.post_title, b.post_date AS d, members.username
FROM (posts) as b
INNER JOIN members ON b.post_by = members.id
WHERE b.post_set = 0)
ORDER BY d DESC LIMIT 5
Try this:
(SELECT a.reply_post, a.reply_content, a.reply_date AS d, members.username
FROM replies AS a
INNER JOIN members ON a.reply_by = members.id)
UNION
(SELECT b.post_id, b.post_title, b.post_date AS d, members.username
FROM posts as b
INNER JOIN members ON b.post_by = members.id
WHERE b.post_set = 0) /* Use where condition after matching Id's using ON */
ORDER BY d DESC LIMIT 5
SELECT DISTINCT a.userID, buildstatus
FROM meeting a
WHERE buildStatus = 'complete'
INNER JOIN user.contactName ON (a.userID = b.userID)
Ok so I'm trying to join these two tables where buildstatus from the meeting table is found to be complete it will then find the user id from the user table and join the contact name to this query.
Unfortunately, whatever I try seems to throw up an error message.
Any ideas how to solve this issue?
The order of the query clauses have to be correct. switch join and where
SELECT DISTINCT a.userID, a.buildstatus
FROM meeting a
INNER JOIN `user` b ON a.userID = b.userID
WHERE a.buildStatus = 'complete'