Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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
Php 如何从表中获取唯一数据?_Php_Mysql_Sql - Fatal编程技术网

Php 如何从表中获取唯一数据?

Php 如何从表中获取唯一数据?,php,mysql,sql,Php,Mysql,Sql,我有一个表,在mysql中有bus_id和cus_id。我想从表中获取B108作为返回值,因为我想要的结果的值不等于C108,并且C108也有B109。所以我也不想要第3行的值。我只想得到第2行作为结果。请建议我选择这些表的查询 我的桌子是: 试试这个: select bus_id from table where bus_id not in (select bus_id from table where cus_id='c108') 我还没有测试过,但像这样的东西可能有用 从bus_id不在

我有一个表,在mysql中有bus_id和cus_id。我想从表中获取B108作为返回值,因为我想要的结果的值不等于C108,并且C108也有B109。所以我也不想要第3行的值。我只想得到第2行作为结果。请建议我选择这些表的查询

我的桌子是:

试试这个:

select bus_id from table where bus_id not in (select bus_id from table where cus_id='c108')

我还没有测试过,但像这样的东西可能有用

从bus_id不在的表中选择bus_id从cus_id=C108的表中选择bus_id您可以根据您的条件使用mysql DISTINCT函数

例如。
从客户id为“C108”和总线id为“B109”的表名称中选择distinctcolumn_名称

您可以单独使用组表,检测每列的重复值,然后使用这些值筛选表

    select * from table 
    where cus_id not in
    (select cus_id from table 
    group by cus_id
    having count(*) > 1)
    and
    bus_id not in
    (select bus_id from table 
    group by bus_id
    having count(*) > 1)
请尝试此查询

CREATE TABLE BUS
    (`cb_id` int, `bus_id` varchar(4), `cus_id` varchar(4))
;

INSERT INTO BUS
    (`cb_id`, `bus_id`, `cus_id`)
VALUES
    (1, 'B101', 'C108'),
    (2, 'B108', 'C101'),
    (3, 'B109', 'C102'),
    (24, 'B109', 'C108')
;




SELECT
    * 
FROM BUS B
WHERE cus_id<>'C108'
AND NOT EXISTS(SELECT * FROM BUS B1 
        WHERE 
             B1.BUS_ID=B.BUS_ID 
         AND B1.cus_id='C108')

如果您的意思是忽略在bus_id或cus_id列中有2个或更多数据的记录,那么您可以使用以下查询:

select * from buscus a where
   a.bus_id not in (select b.bus_id from (select count(bus_id) as cb, bus_id FROM BusCus group by bus_id) b where    b.cb > 1) 
and
   a.cus_id not in (select c.cus_id from (select count(cus_id) as cc, cus_id FROM BusCus group by cus_id) c where c.cc >    1)

.

安德鲁和我有点重叠