I'm using MySQL 5.7 and have a query below:
SELECT
U.*,
C.country,
C.id AS country_id,
C.iso
FROM
`users` AS U
LEFT JOIN
`countries` AS C
ON
U.user_country_id = C.id
WHERE
U.user_deleted = 0
GROUP BY
U.user_country_id
HAVING
MAX(U.user_credits)
The problem is:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'dbname.U.id' which is not functionally dependent on columns in GROUP BY clause;
I can't change my group (wrong results) to: GROUP BY U.user_country_id, U.id
How can I change my request the right way?
Thanks!
You can obtain the row with the max(U.user_credits) this way
SELECT
U.*,
C.country,
C.id AS country_id,
C.iso
FROM
`users` AS U
LEFT JOIN
`countries` AS C
ON
U.user_country_id = C.id
WHERE
U.user_deleted = 0
AND U.user_credits = ( select max(U.user_credits) from `users` as U)
Related
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
I've got the following SQL query and I'm trying to implement pagination, so I first want to get the COUNT of the result:
The normal query (works fine)
SELECT DISTINCT c.*, p1.*, username FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL ORDER BY c.firstname ASC
I've tried the following, but it throws an error and I'm not sure what correct syntax to use:
Attempting to count the results (doesn't work)
SELECT COUNT(DISTINCT c.*, p1.*, username) FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL ORDER BY c.firstname ASC
The error:
Syntax error or access violation: 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 ', p1., username) FROM candidate c LEFT ' at line 1
One option is to use a subquery:
SELECT COUNT(*)
FROM (
SELECT DISTINCT c.*, p1.*, username FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL
) t
Depending on your data, you may be able to do this without the subquery, but you cannot use multiple columns with the count aggregate -- that's what is causing your error.
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
I am wondering about another syntax error:
SELECT * FROM forum_question
WHERE category='art'
LEFT JOIN forum_answer ON (forum_question.id = forum_answer.question_id)
GROUP BY forum_question.id
ORDER BY a_datetime DESC;
The error I am getting is this:
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 'LEFT JOIN forum_answer ON (forum_question.id =
forum_answer.question_id) GROUP B' at line 1
Thanks.
The WHERE clause should be after the FROM clause. JOIN statements are part of the FROM clause
SELECT *
FROM forum_question
LEFT JOIN forum_answer
ON (forum_question.id = forum_answer.question_id)
WHERE category='art'
GROUP BY forum_question.id
ORDER BY a_datetime DESC;
The WHERE clause should be after the JOIN clause
SELECT *
FROM forum_question
LEFT JOIN forum_answer
ON (forum_question.id = forum_answer.question_id)
WHERE category='art'
GROUP BY forum_question.id
ORDER BY a_datetime DESC;
First join, then use the where clause:
SELECT * FROM forum_question
LEFT JOIN forum_answer ON (forum_question.id = forum_answer.question_id)
WHERE forum_question.category='art'
GROUP BY forum_question.id
ORDER BY a_datetime DESC;