Php 从不同的mySQL表中添加值
我的桌子:Php 从不同的mySQL表中添加值,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我的桌子: cloe_Pictures: id | title | mainKategorie 1001 | ABC | 1 1002 | BCD | 1 1003 | need | 2 1004 | FGH | 3 cloe_htbl_Kategorie: bezeichnung | kategorieId Kat1 | 1 BLO | 2
cloe_Pictures:
id | title | mainKategorie
1001 | ABC | 1
1002 | BCD | 1
1003 | need | 2
1004 | FGH | 3
cloe_htbl_Kategorie:
bezeichnung | kategorieId
Kat1 | 1
BLO | 2
Peng | 3
yxz | 4
cloe_Kategorie:
picID | kategorieId
1003 | 1
1005 | 2
2003 | 3
3003 | 4
以下是查询:
SELECT pic.title, kat.bezeichnung AS kategorie
FROM cloe_Pictures pic, cloe_htbl_Kategorie kat
WHERE pic.mainKategorie=kat.kategorieId
AND pic.mainKategorie=1
给出:
title | kategorie
ABC | Kat1
BCD | Kat1
我的问题是:如何包含ID=1的cloe_Kategorie表中的所有记录
我最后有一张这样的桌子:
title | kategorie
ABC | Kat1
BCD | Kat1
need | Kat1
你还需要在克洛伊·卡泰戈里和克洛伊·图尔斯之间进行连接。像下面这样
SELECT pic.title, kat.bezeichnung AS kategorie
FROM cloe_Pictures pic, cloe_htbl_Kategorie kat
WHERE pic.mainKategorie=kat.kategorieId AND pic.mainKategorie=1
UNION
SELECT pic.title, kat.bezeichnung AS kategorie
FROM cloe_htbl_Kategorie kat JOIN cloe_Kategorie ck
ON kat.kategorieId = ck.kategorieId
AND ck.picID = 1003
JOIN cloe_Pictures pic ON pic.id = ck.picID
抱歉:这个查询有一个空结果。我的下一个想法是做两个查询。选择*来自主卡泰格里=1的cloe_图片和:选择*来自卡泰格里ID=1的cloe_卡泰格里图片。把它们放在一起…给了很多不符合逻辑的记录content@hamburger,问题是我没有尝试。只是需要正确地设置条件。现在试试。请看,我已移动条件ck.picID=1003i确认此答案正确,我已创建模式并执行此qry。