Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql:选择具有多个值的id_Mysql_Select - Fatal编程技术网

Mysql:选择具有多个值的id

Mysql:选择具有多个值的id,mysql,select,Mysql,Select,我有表1: +------+---------+ | id | name | +------+---------+ | 1 | name1 | | 2 | name2 | | 3 | name3 | | 4 | name4 | +------+---------+ 我有表2: +------+---------+ | id | object | +------+---------+ | 1 | 4 | | 1 |

我有表1:

+------+---------+
| id   | name    |
+------+---------+
| 1    | name1   |
| 2    | name2   |
| 3    | name3   |
| 4    | name4   |
+------+---------+
我有表2:

+------+---------+
| id   | object  |
+------+---------+
| 1    |    4    |
| 1    |    8    |
| 2    |    23   |
| 2    |    8    |
| 2    |    9    |
| 3    |    2    |
| 3    |    8    |
| 4    |    9    |
| 4    |    23   |
+------+---------+
我想选择name=name2,它有对象8,23,但忽略另一个id,它只有对象8或对象23,结果将是:

+------+---------+
| id   | name    |
+------+---------+
| 2    | name2   |
+------+---------+
谢谢你的帮助,大师。

试试这个:

select a.id, a.name
from table1 a
where exists (select 1 from table2 c
              where object = 8 and c.id = a.id)
and exists (select 1 from table2 c
            where object = 23  and c.id = a.id)
那么:

SELECT DISTINCT table1.id, table1.name FROM table1, table2 WHERE table1.name = "name2" AND
table1.id = table2.id and (table2.object = 8 OR table2.object = 23);
当您想要找到一个id有两个不同的值时,可以使用自连接

你能试试这个吗?你可以在这里测试


到目前为止你有没有尝试过?你知道如何计算聚集和分组的工作吗?至少,考虑提供适当的DDL和/或SqLFIDLE连同期望的结果集-以及你迄今为止的最大努力。
SELECT *
FROM
(
    SELECT a.id
    FROM table2 AS a INNER JOIN table2 AS b
      ON a.id = b.id
    WHERE a.object = 8 AND b.object = 23
) x INNER JOIN table1 ON x.id = table1.id;