Php MYSQL或一列上的两个值
我有两张桌子 表1:tA 表2:结核病 预期产量 mysql代码: 我的mysql代码的结果:Php MYSQL或一列上的两个值,php,mysql,sql,Php,Mysql,Sql,我有两张桌子 表1:tA 表2:结核病 预期产量 mysql代码: 我的mysql代码的结果: 怎么办?按tA_id删除您的组,因为它将cat_name Mobile和其他手机分组在一起 如果您需要按tB.cat_id ASC使用订单,我相信它将显示您需要的cat_id 1和2查询中有错误。。。缺少JOIN 查询 这正在获取预期的结果 +----+---------------------+-----+--------+--------+----------+ | id | url
怎么办?按tA_id删除您的组,因为它将cat_name Mobile和其他手机分组在一起
如果您需要按tB.cat_id ASC使用订单,我相信它将显示您需要的cat_id 1和2查询中有错误。。。缺少JOIN 查询 这正在获取预期的结果
+----+---------------------+-----+--------+--------+----------+
| id | url | id | tA_id | cat_id | cat_name |
+----+---------------------+-----+--------+--------+----------+
| 1 | hxxp://www.aaa.com | 1 | 1 | 1 | mobile |
| 2 | hxxp://www.bbb.com | 3 | 2 | 2 | other |
+----+---------------------+-----+--------+--------+----------+
试试这个:-
select *
from tA
inner join tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
group by tb.cat_name;
希望它能帮助您。但我的数据库中有300多条记录。如果我使用“按类别名称分组”。我的结果显示了2条记录。请尝试按类别名称分组。
+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 1 | Mobile |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
select *
from tA
inner tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
Group By tA_id
+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 2 | Other |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
SELECT *
FROM tA
INNER JOIN tB
ON tA.id = tB.tA_id
WHERE tB.cat_id = 1
OR tB.cat_id = 2
GROUP BY tA_id
+----+---------------------+-----+--------+--------+----------+
| id | url | id | tA_id | cat_id | cat_name |
+----+---------------------+-----+--------+--------+----------+
| 1 | hxxp://www.aaa.com | 1 | 1 | 1 | mobile |
| 2 | hxxp://www.bbb.com | 3 | 2 | 2 | other |
+----+---------------------+-----+--------+--------+----------+
select *
from tA
inner join tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
group by tb.cat_name;