php - MYSQL OR 一列上的两个值

标签 php mysql sql

我有 2 张 table 。

表 1:tA

+----+--------------------+
| id | url                |
+----+--------------------+
| 1  | hxxp://www.aaa.com |
| 2  | hxxp://www.bbb.com |
+----+--------------------+

表 2:待定

+----+-------+--------+----------+
|id  | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1  |   1   |    1   |  Mobile  |
| 2  |   1   |    2   |  Other   |
| 3  |   2   |    2   |  Other   |
+----+-------+--------+----------+

预期输出

+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1  |   1   |    1   |   Mobile |
| 3  |   2   |    2   |   Other  |
+----+-------+--------+----------+

mysql代码:

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

我的 mysql 代码的结果:

+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1  |   1   |   2    |   Other  |
| 3  |   2   |   2    |   Other  |
+----+-------+--------+----------+

我该怎么办?

最佳答案

按 tA_id 删除您的组,因为它将 cat_name Mobile 和其他分组在一起。

如果您需要保留它,请使用ORDER BY tB.cat_id ASC,我相信它会显示您需要的cat_id 1和2

关于php - MYSQL OR 一列上的两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945308/

相关文章:

php - 如何在 PHP 中将两个字符串组合在一起?

php - 查询字符串中未捕获异常

php - 菜鸟在这里 : I'm having trouble understanding the logic behind this regular expression search

PHP Flush 有效……即使在 Nginx 中

php - 使用php从sql数据库创建json对象

php - 将html表内容移动到sql数据库

sql - 使用IN从sqlite数据库删除

java - 日期值设置为 NULL 时的原始 SQL 更新语句

php - fatal error :调用未定义的方法 db::_results()

php - fatal error : Call to undefined function mysqli_connect() in. .. 将 PHP 5.4.22 和 MySQL 5.5 与 Apache 2.4.7 连接时