我想创建一个依赖于其他两个表的SELECT查询
表1: tbcategories
id name
-- ----
1 category1
2 category2
3 category3
表2: tbgroups
id category name
-- -------- ----
1 1 group1
2 1 group2
3 2 group3
表3: tbchilds
id group name
-- ----- ----
1 group1 child1
2 group1 child2
3 group2 child3
4 group2 child4
5 group3 child5
我需要什么 - 查询语法给了我所有的孩子(tbchilds)他们的"组"是特定类别。 例如:给我所有的孩子""" category1 =输出将是:
child1
child2
child3
child4
10X
答案 0 :(得分:1)
SELECT *
FROM tbcategories a
INNER JOIN
tbgroups b
ON
a.id = b.category
JOIN
tbchilds t3
ON
t2.name = t3.group
WHERE
t1.name ='category1'
答案 1 :(得分:0)
这个未经测试的查询应该能得到预期的结果:
select t3.name from tbcategories t1 join tbgroups t2 on t1.id = t2.category join tbchilds t3 on t2.name=t3.`group` where t1.name ='category1'
答案 2 :(得分:0)
Best example for PHP MySQL joins
<强>尝试强>
SELECT ch.name FROM tbchilds ch
JOIN tbgroups gr ON ch.group=gr.name
JOIN tbcategories cat ON cat.id=gr.category
WHERE cat.name='category1';
答案 3 :(得分:0)
您可以尝试这样的事情:
SELECT C.name
FROM tbcategories A
INNER JOIN tbgroups B
ON A.id = B.category
INNER JOIN tbchilds C
ON B.name = C.group
WHERE A.name = 'CATEGORY1';
希望这对你有所帮助。
答案 4 :(得分:0)
使用以下查询 -
SELECT CH.name FROM tbgroups AS G
INNER JOIN tbchilds AS CH ON CH.group = G.id
INNER JOIN tbcategories AS C ON C.id = G.category
WHERE C.id = <CategoryID>;