How to add limit and offset to sql query? - mysql

I have this sql query but it does not work:
SELECT u.id, u.first_name, u.last_name
, m.media_link, g_m.member_type
FROM Group_Members g_m
LEFT JOIN Users u
ON g_m.user_id = u.id
LEFT JOIN Media m
ON u.id = m.user_id
WHERE g_m.group_id = ?
AND m.position = ?
AND m.type = ?
ORDER BY u.first_name ASC LIMIT ?, 50;
This is the error I get
get memberFound error: Error: ER_PARSE_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 ''0', 50' at line 1
GET /group_members?group_id=6&offset=0
Can anyone help, not sure how to add limit and offset correctly, this query worked without the Limit/offset add...
This is mysql and nodejs.

I don't see any syntax errors in your SQL.
Use a setInt method for the LIMIT question mark.
SELECT
u.id,
u.first_name,
u.last_name,
m.media_link,
g_m.member_type
FROM
Group_Members g_m
LEFT JOIN Users u ON g_m.user_id = u.id
LEFT JOIN Media m ON u.id = m.user_id
WHERE
g_m.group_id = ?
AND m.position = ?
AND m.type = ?
ORDER BY
u.first_name ASC
LIMIT
?, 50;

Related

Sql query question finding out table name from sql query

I am testing one of my project it's opencart
Basically i wanna know my table name for sure for example oc20_product and oc20_manufacturer are table names aren't them ? and this the others actual columns ?
What is my table and column name for sure ?
Thanks in advance
Notice:
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 '%' AND p.status = 1 GROUP BY m.manufacturer_id ORDER BY m.name
ASC' at line 4 Error No: 1064
SELECT m.manufacturer_id, m.name, COUNT(p.product_id) AS products_total
FROM oc20_product p
LEFT JOIN oc20_product_description pd
ON pd.product_id = p.product_id
LEFT JOIN oc20_manufacturer m
ON m.manufacturer_id = p.manufacturer_id
WHERE pd.name LIKE '%mycard'%' AND p.status = 1
GROUP BY m.manufacturer_id
ORDER BY m.name ASC in
wrong quote in like pattern you could use a concat for an easy pattern format
SELECT m.manufacturer_id
, m.name
, COUNT(p.product_id) AS products_total
FROM oc20_product p
LEFT JOIN oc20_product_description pd ON pd.product_id = p.product_id
LEFT JOIN oc20_manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE pd.name LIKE concat('%',mycard,'%') AND p.status = 1
GROUP BY m.manufacturer_id, m.name
ORDER BY m.name ASC

SELECT query IF CONDITION

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

IF within WHERE

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

MYSQL query using Left Join and Where IN clause

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

Upper table visibility in select with multiple subqueries

I have a query:
SELECT b.user_id, b.active FROM users b
WHERE b.followers_count != (SELECT COUNT(*)
FROM (SELECT u.user_id
FROM user_follow uf,users u,user_follow_request ufr
WHERE
uf.following_id = b.user_id AND
uf.following_id = ufr.friend_id AND
ufr.status = 'approved' AND
ufr.user_id = u.user_id AND u.user_id != b.user_id AND u.active != 0
GROUP BY u.user_id) a)
AND b.active = -1 limit 5;
It has to select all the user_id's from users that have different followers_count in the column from the one being calculated by sql.
But the problem is that I'm receiving error message
Error Code: 1054. Unknown column 'b.user_id' in 'on clause'
What I'm doing wrong? Highly appreciate your help.
Did you mean uf.user_id = b.user_id instead of uf.following_id = b.user_id? Anyway, what user_follow table is for?
Agree to Strawberry, you'd better use explicit joins. They are much easier to read and understand. Here is what you may have mentioned (note that user_follow table is not used at all):
SELECT u.user_id, u.followers_count, IF(ISNULL(uafc.actual_fc),0,uafc.actual_fc) AS afc
FROM users AS u
LEFT JOIN (
SELECT u.user_id AS user_id, COUNT(*) AS actual_fc
FROM users AS u
JOIN user_follow_request AS ufr
ON ufr.friend_id = u.user_id
JOIN users AS fu
ON fu.user_id = ufr.user_id
WHERE ufr.user_id != u.user_id
AND ufr.status = 'approved'
AND fu.active != 0
GROUP BY u.user_id, u.followers_count
) AS uafc
ON uafc.user_id = u.user_id
WHERE u.followers_count != IF(ISNULL(uafc.actual_fc),0,uafc.actual_fc)
;
There may be some nicer solution for NULL workaround.
See it on SQLFiddle: http://sqlfiddle.com/#!2/71f6c/3