Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will i join this two query in one single query?

I have this first query.

SELECT item.id, item.name, item.description, 
       item.tnURL, item.spec_id, item.item_type, item.sub_category  
FROM item, fb_item_promo 
WHERE fb_item_promo.item_id=item.id;

And the second query.

SELECT item.id, item.name, item.description, 
       item.tnURL, item.spec_id, item.item_type, item.sub_category  
FROM item 
WHERE item.description LIKE '%package%';

I need to combine the first query result plus the second query result. How can i do it on a much faster way?

@Fosco your query seems to work but what if i want to add a search function to all the results on item.name? i tried this one but it still gets all fb_item_promo.item_id results.

New Query:

SELECT item.id, item.name, item.description, item.tnURL,item.spec_id, 
 item.item_type, item.sub_category 
FROM item 
LEFT JOIN fb_item_promo 
ON fb_item_promo.item_id = item.id 
WHERE fb_item_promo.item_id IS NOT NULL 
OR item.description LIKE '%package%' 
AND item.name LIKE '%my item name%'

The result is it search for the right item but still return all the items on fb_item_promo.item_id that is present. How can i run a filter on that result?

like image 591
tiltdown Avatar asked Nov 20 '25 08:11

tiltdown


2 Answers

It doesn't make sense to include ALL records from the first query, PLUS the records from the second query. The records from the second query will already have been included in the first.

I think this is what you need:

SELECT item.id, item.name, item.description, 
       item.tnURL, item.spec_id, item.item_type, 
       item.sub_category 
FROM item
left JOIN fb_item_promo ON fb_item_promo.item_id=item.id
WHERE fb_item_promo.item_id is not null
  or item.description LIKE '%package%'
like image 163
Fosco Avatar answered Nov 21 '25 23:11

Fosco


SELECT item.id, item.name, item.description, item.tnURL,
item.spec_id, item.item_type, item.sub_category 
FROM 
item, fb_item_promo WHERE fb_item_promo.item_id=item.id;

UNION ALL

SELECT item.id, item.name, item.description, item.tnURL, 
item.spec_id, item.item_type, item.sub_category
FROM 
item WHERE item.description LIKE '%package%';
like image 43
DRiFTy Avatar answered Nov 21 '25 21:11

DRiFTy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!