如何将两个MySQL查询合并在一起

时间:2011-12-28 15:25:27

标签: php mysql

如何将以下查询合并在一起?

要获取特定类型的所有对象,我使用

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

我也有这个搜索标题

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

如何才能获得具有特定标题的子组23的对象?

3 个答案:

答案 0 :(得分:4)

两张表有何关联?如果他们都引用了您内部加入的ID并使用AND来组合条件:

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

答案 1 :(得分:2)

如果您的social_objects_singleID表具有相同的social_objects,则可以执行以下操作:

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;

答案 2 :(得分:0)

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