如何在一个查询中查询两个表?

时间:2010-12-25 22:37:15

标签: sql

如何合并/合并此查询:

SELECT pages.*, pages_content.title, 
    pages_content.label FROM pages, 
    pages_content WHERE
    pages.menu='top' AND pages_content.parent = 
    pages.ID order by 
    pages.sequence ASC"

进入这一个:

"SELECT a.ID, a.parent, a.title, a.label, a.link, a.sequence, Deriv1.Count  FROM 
pages a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM 
pages WHERE menu='top' GROUP BY parent) Deriv1 ON a.ID = Deriv1.parent 
WHERE a.parent=" . $parent1 . " AND menu='top' order by sequence ASC"

最后一个查询有效,但我需要将标题和标签数据替换为另一个表(pages_content),如第一个查询中所做的那样

地球上如何将两者组合或两者合二为一?左外连接

1 个答案:

答案 0 :(得分:0)

现在,进行第二次查询

-- Original select clause
SELECT a.ID, a.parent, a.title, a.label, a.link, a.sequence, Deriv1.Count  
-- Add additional columns
,b.title,b.label_data
FROM pages a  
-- Add the page_content table
JOIN pages_content b on b.parent=a.id
LEFT OUTER JOIN 
(SELECT parent, COUNT(*) AS Count FROM pages 
        WHERE menu='top' GROUP BY parent) Deriv1 ON a.ID = Deriv1.parent 
WHERE a.parent=" . $parent1 . " AND menu='top' 
order by sequence ASC"