user2951112 user2951112 - 4 months ago 9
SQL Question

Getting stuck using less than operator in mysql query

I need just only data by

pbox.id<'".$pbid."' and cbox.id<'".$cbid."'
but it does not work. I tried a lot of things still not work, it gets all data. It not look my less than operators. How I solve this issue?

SELECT DISTINCT pbox.id as pbid,
cbox.id as cbid,
box.upload_type,
box.id,
box.box_type,
box.page_name,
box.title,
box.connect,
box.type,
box.uid,
box.description,
box.image,
box.url,
box.status,
box.date,
box.time
FROM boxes as box
LEFT JOIN page_boxes as pbox on pbox.bid=box.id
JOIN page_subcribers as pages on pages.page_id=pbox.page_id
LEFT JOIN category_boxes as cbox on cbox.bid=box.id
LEFT JOIN subcribers as catsb on cbox.category_id=catsb.cid
WHERE pages.uid='".$session_id."' or catsb.uid='".$session_id."' and box.status='".$approval."' and cbox.id<'".$cbid."' and pbox.id<'".$pbid."'
ORDER BY pbox.id desc,cbox.id DESC LIMIT 10

Answer

Try forcing the operator precedence using brackets on the WHERE clause:-

SELECT DISTINCT pbox.id as pbid,
cbox.id as cbid,
box.upload_type,
box.id,
box.box_type,
box.page_name,
box.title,
box.connect,
box.type,
box.uid,
box.description,
box.image,
box.url,
box.status,
box.date,
box.time
FROM boxes as box
LEFT JOIN page_boxes as pbox on pbox.bid=box.id
JOIN page_subcribers as pages on pages.page_id=pbox.page_id 
LEFT JOIN category_boxes as cbox on cbox.bid=box.id 
LEFT JOIN subcribers as catsb on cbox.category_id=catsb.cid 
WHERE (pages.uid='".$session_id."' 
or catsb.uid='".$session_id."')
and box.status='".$approval."' 
and cbox.id<'".$cbid."' 
and pbox.id<'".$pbid."'
ORDER BY pbox.id desc,cbox.id DESC 
LIMIT 10