开发者

How can I query two tables in one query?

开发者 https://www.devze.com 2023-02-01 08:46 出处:网络
How do I merge/combinethis query: SELECT pages.*, pages_content.title, pages_content.label FROM pages,

How do I merge/combine this query:

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"

Into this one:

"SEL开发者_StackOverflowECT 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"

The last query works but i need to replace the title and label data form another table (pages_content) as is done in the first query

How on Earth can I combine the two or fit in two? left outer joins


Now, to make the second query

-- 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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号