I wrote a query with a IF() statement within WHERE clause:
SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name
(
SELECT b.date_recorded
FROM wp_bp_activity as b
WHERE b.type IN ( 'activity_comment','activity_update' )
AND IF(b.type = 'activity_comment', b.item_id, a.id) = a.id
ORDER BY b.item_id desc
limit 0,1
) as drecord
FROM wp_bp_activity as a
LEFT JOIN wp_users as u ON a.user_id = u.ID
WHERE
a.type IN ( 'activity_update' )
order by cast(drecord as datetime) desc
limit 0,20
But it gives 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 'SELECT b.date_recorded FROM wp_bp_activity as b WHERE b.' at line 3
What's the correct way of using IF like that?
First thing to change is to add comma after u.display_name. It should help.
You miss a comma after u.display_name ... query will be
SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name,
(
SELECT b.date_recorded
FROM wp_bp_activity as b
WHERE b.type IN ( 'activity_comment','activity_update' )
AND IF(b.type = 'activity_comment', b.item_id, a.id) = a.id
ORDER BY b.item_id desc
limit 0,1
) as drecord
FROM wp_bp_activity as a
LEFT JOIN wp_users as u ON a.user_id = u.ID
WHERE
a.type IN ( 'activity_update' )
order by cast(drecord as datetime) desc
limit 0,20
hope this fix the issue... as I didn't check for other errors.
Do you even need the if ?
((b.type = 'activity_comment' AND b.item_id = a.id) OR (b.type <> 'activity_comment'))
seems to be what you are trying to do
The SQL error like others have said though is the missing comma
Related
I have this query to group together article authors on my site, along with how many articles they've done recently and their most recent posted date.
The problem is this error:
#1055 - Expression #4 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'goltest.a.author_id' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
SELECT
COUNT(DISTINCT a.article_id) AS `counter`,
u.username,
u.user_id,
(
SELECT
`date`
FROM
`articles`
WHERE
`author_id` = a.`author_id`
ORDER BY
`article_id`
DESC
LIMIT 1
) AS `last_date`
FROM
`articles` a
LEFT JOIN
`users` u
ON
u.user_id = a.author_id
LEFT JOIN
`article_category_reference` c
ON
a.`article_id` = c.`article_id`
WHERE
a.`date` >= 1491004800 AND a.`date` <= 1495018102 AND a.`active` = 1 AND c.`category_id` NOT IN(63) AND a.author_id != 1844
GROUP BY
u.`username`,
u.`user_id`
ORDER BY
`counter`
DESC
,
a.date
DESC
Don't really understand why it wants "author_id" in the group by list, since I'm not selecting it?
For those interested I fixed it with this, replacing the subquery with MAX() value for the date:
SELECT
COUNT(DISTINCT a.article_id) AS `counter`,
u.username,
u.user_id,
MAX(a.date) AS `last_date`
FROM
`articles` a
LEFT JOIN
`users` u
ON
u.user_id = a.author_id
LEFT JOIN
`article_category_reference` c
ON
a.`article_id` = c.`article_id`
WHERE
a.`date` >= 1491004800 AND a.`date` <= 1495018102 AND a.`active` = 1 AND c.`category_id` NOT IN(63) AND a.author_id != 1844
GROUP BY
u.`username`,
u.`user_id`
ORDER BY
`counter`
DESC
,
last_date
DESC
With thanks to Shadow for pointing out "max" :)
I have this query:
SELECT *,
COALESCE((
SELECT SUM(sum_points)
FROM (
SELECT SUM(user_points.points) AS sum_points FROM user_points
WHERE user_points.user_id = user.user_id
UNION ALL
SELECT SUM(used_points.points) AS sum_points FROM used_points
WHERE used_points.user_id = user.user_id
) t
), 0) AS total_points_credit
FROM user
But i get this error:
#1054 - Unknown column 'user.user_id' in 'where clause'
How can i user the user.user_id value in WHERE condition in the subqueries?
Thank you.
Try this way:
SELECT *,
COALESCE((SELECT SUM(user_points.points)
FROM user_points
WHERE user_points.user_id = user.user_id), 0)
+
COALESCE((SELECT SUM(used_points.points)
FROM used_points
WHERE used_points.user_id = user.user_id), 0) AS total_points_credit
FROM user
I don't think that you need to have all of these subqueries. I feel like you can likely do what you're looking to do with:
SELECT A.user_id, SUM(B.points) + SUM(C.points) total_points_credit FROM user A
JOIN user_points B ON
A.user_id = B.user_id
JOIN used_points C ON
A.user_id = C.user_id
GROUP BY A.user_id
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
The following mysql query...
SELECT a.*, b.*,
(
SELECT COUNT( * )
FROM lp_units c
WHERE c.property_id = a.property_id
) AS unitcount
FROM lp_property a,
lp_property_confidential b
WHERE a.property_id = b.property_id
AND c.unitcount<= a.no_of_units
AND a.account_id = '1'
returns an error...
Unknown column 'c.unitcount' in 'where clause'
I think my query would be understandable. solve it to run....
Thanks in advance...
Don't use c.unitcount. Just unitcount. unitcount is not a column of c but rather of the temporary table generated by the subquery.
However, this query is probably better written as a join anyway.
Try this query
SELECT
a.*,
b.*,
COUNT(c.property_id) as unitcount
FROM lp_property a
JOIN lp_property_confidential b ON a.property_id = b.property_id
JOIN lp_units c ON c.property_id = a.property_id
WHERE
a.account_id = '1'
GROUP BY a.property_id
HAVING unitcount <= a.no_of_units
SELECT 'Q' AS TYPE,
q.question AS value,
q.date
FROM questions q
WHERE q.user_id =39
UNION ALL
SELECT 'A' AS TYPE,
q.question AS value,
a.date
FROM answers a,
questions q
WHERE a.question_id = q.id
AND WHERE a.user_id =39
ORDER BY `date` DESC
database design:
questions{id,user_id,question,date}
answers{id,question_id,user_id,answer,date}
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 'WHERE a . user_id = 39 ORDER BY date DESC
There are two WHERE-clauses in the lower part of the query. You need to literally remove just the last word WHERE. The error description is all you need.
"WHERE" appears twice. Where x = ..... AND ....
Had an extra where a.user_id=39. You only use the keyword WHERE at the beginning of the clause (a.question_id = q.id AND a.user_id = 39)
SELECT
'Q' AS TYPE , q.question AS value, q.date
FROM questions q
WHERE q.user_id =39
UNION ALL
SELECT 'A' AS TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.question_id = q.id
AND
a.user_id =39
ORDER BY `date` DESC
As the error tries to explain: For the second select you only need to put one "WHERE".
SELECT 'A' AS TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.question_id = q.id
AND
a.user_id =39