MySQL如何在此查询中创建一个select子查询

时间:2014-02-07 20:14:38

标签: mysql sql

如何通过利用子查询将这两个查询转换为一个查询?

select p.id, m.display_name, m.updated_at, group_concat(meta.value) as value, p.path
from metadata_custom meta
right join page as p on p.id = meta.page_id
inner join metadata m on p.metadata_id = m.id
where p.path like '/history/%' and p.path not like '/history/%/%'
group by p.id

SELECT * FROM cascade_publish.folder
where path like '/history/%' and path not like '/history/%/%'

我见过的大多数子查询都使用WHERE IN .... 我最好能够将第二个查询放在第一个查询的某个地方,我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

一种方法是使用子查询作为选择字段:

SELECT p.id
 ( SELECT folder FROM cascade_publish
   where path like '/history/%' and path not like '/history/%/%') as publish_folder
FROM ...

答案 1 :(得分:0)

您可以在表格通常位置的任何位置添加带有别名的SELECT。例如:

SELECT * FROM table
JOIN (SELECT * FROM other_table WHERE x = 10) AS temp

或者,像这样:

SELECT SUM(temp.value) FROM (SELECT MAX(value) AS value FROM my_table GROUP BY whatever) AS temp