如何从表A的一行和表B的多行中进行选择?
问题描述:
我有两个桌子.
页面
+-----------+----------+------------+
| ID | title | URL |
+-----------+----------+------------+
| 1 | test | test.html |
| 2 | test2 | test2.html |
+-----------+----------+------------+
文件
+-----------+----------+------------+
| ID | page_id | name |
+-----------+----------+------------+
| 10 | 1 | a.jpg |
| 11 | 1 | b.jpg |
| 12 | 2 | c.jpg |
+-----------+----------+------------+
如何从PAGES中选择一行并在FILES中选择多行?
How to select from PAGES one row and FILES multi rows??
我的查询为:
select * from pages,files WHERE (pages.id = page_id) AND (url='$url')
以上查询的输出:
test
a.jpg
我需要的输出:
test
a.jpg
b.jpg
答
这比起专门用于PHP的问题,更多的是SQL问题.
我认为这就是您想要的,但是我不确定您的措辞.
This is more of a sql question than anything to do with specifically PHP.
I think this is what you want, but I'm not sure with your wording.
SELECT pages.title, files.name
FROM pages
INNER JOIN files ON pages.id = files.page_id
WHERE (pages.url='$url')
GROUP BY files.name;