简体   繁体   中英

How to merge two MySQL queries together

How can I merge the following queries together?

To get all the objects of a particular type I use

SELECT ID FROM social_objects  
WHERE subgroup='23' ORDER BY time_created DESC LIMIT 0 , 30 

I have this search too, for titles

SELECT ID FROM 'social_objects_single' 
WHERE 'title' LIKE '%indian%' LIMIT 0 , 30 

How can I get only objects of subgroup 23 with certain titles?

How are the two tables related? If they both reference an ID you inner join and use AND to combine conditions:

SELECT Parent.ID, Child.ID 
FROM ParentTable 
INNER JOIN ChildTable ON ParentTable.ID = ChildTable.ForeignKeyID 
WHERE Parent.ID = 23 AND Title LIKE '%indian%'

If your social_objects_single has the same ID as the social_objects table you could do this:

SELECT so.ID FROM social_objects so 
INNER JOIN social_objects_single soi ON soi.ID = so.ID
WHERE so.subgroup = 23 AND soi.title LIKE '%indian%'
ORDER BY so.time_created DESC LIMIT 0, 30;

SELECT SO.ID,SOS.ID
FROM social_objects SO ,social_objects_single SOS
WHERE SOS.title LIKE '%indian% and SO.subgroup=23 and SOS.id =SO.subgroup_id

您应该在连接表时替换最后一个条件SOS.id = SO.subgroup_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM