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;
Related
I have one problem with my SELECT query in my blog page.
I want comment count of each blog when comment status=1.
I am apply following query..
SELECT CONCAT(u.first_name," ",u.last_name) name, r.*,
IF(c.status=1,COUNT(c.id)) as comment
FROM users u
RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
but it giving SYNTAX ERROR..
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your
SQL syntax; check the manual that corresponds to your MariaDB server version for the right
syntax to use near ') as comment FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOI' at line 1
Please tell me where I am wrong.
Thanks
If statement contains three expressions. First, the expression, second the value returned if condition is true and third if condition is false so you are missing the third expression.
Try the below code
SELECT CONCAT(u.first_name," ",u.last_name) name,r.*,IF(c.status=1,COUNT(c.id), 0) as comment
FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
Select concat(u.first_name," ",u.last_name) name,r.*,
case when c.status=1 then COUNT(c.id) end as comment
FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
https://www.w3schools.com/sql/func_mysql_case.asp
if function requires 3 parameters to be passed to it. IF(expression ,expr_true, expr_false) is how it should be used.
Have a look at https://www.w3resource.com/mysql/control-flow-functions/if-function.php
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)
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 have the following query:
SELECT s.username FROM `instagram_shop` s
INNER JOIN `instagram_shop_picture` p
ON s.id = p.shop_id
WHERE s.`deletedAt` IS NULL
HAVING COUNT(p.id) = 0
GROUP BY s.id
and I keep getting the following error:
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 'GROUP BY s.id LIMIT 0, 30' at line 6
What is the issue here?
Move the HAVING clause after the GROUP BY clause.
MySQL is very specific about the order of keywords/clauses in a SELECT statement
Reference: 13.2.9 SELECT Syntax https://dev.mysql.com/doc/refman/5.6/en/select.html
Try the below one :
there is an syntax issue with your query,
Having should follow after GROUP BY caluse.
Also the columns which you mention in your GROUP BY
clause should be present in the SELECT list.
SELECT s.id,s.username FROM `instagram_shop` s
INNER JOIN `instagram_shop_picture` p
ON s.id = p.shop_id
WHERE s.deletedAt IS NULL
GROUP BY s.id,s.username
HAVING COUNT(s.id) = 0
I have three tables A B C and i'm trying to retrieve information from all three.
A has the columnns userid avatar username and B has the column postid, dateshared and C has the column commenter postid datecommented.
I'm trying to run the query
Select C.comment, C.commenter, C.datecommented, B.postid, B.dateshared A.username A.avatar from B Left Join C Left join A on C.postid = B.postid AND A.userid = C.commenter where B.postid IN ('1','2','3') order by C.dateshared desc
but it gives the following error:
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 'where B.postid IN ('1', '2', '3') order by C.dateshared '
Can anyone point out what I'm doing wrong or suggest how to go about it?
Each LEFT JOIN requires its own ON condition:
SELECT C.comment, C.commenter, C.datecommented, B.postid, B.dateshared, A.username A.avatar
FROM B
LEFT JOIN
C
ON C.postid = B.postid
LEFT JOIN
A
ON A.userid = C.commenter
WHERE B.postid IN ('1','2','3')
ORDER BY
C.dateshared desc
This should work for you, your query had some syntax errors:
Select C.comment,C.commenter,C.datecommented,B.postid,B.dateshared,A.username,A.avatar
from B
Left Join C on C.postid = B.postid
Left join A on A.userid = C.commenter
where B.postid IN ('1','2','3')
order by C.dateshared desc