我有一个在mysql中有bus_id和cus_id的表。我想从表中得到B108作为返回值,因为我想要的结果的值不等于C108而C108也有B109。所以我也不想要第3行的价值。我希望只得到第2行作为我的结果。请建议我这些表的选择查询。
我的表是:
答案 0 :(得分:1)
您可以单独使用组表并检测每列的重复值,然后使用这些值过滤表。
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)
答案 1 :(得分:0)
试试这个:
select bus_id from table where bus_id not in (select bus_id from table where cus_id='c108')
答案 2 :(得分:0)
我还没有测试过,但这样的事情可能有用
SELECT bus_id FROM table WHERE bus_id NOT IN(SELECT bus_id FROM table WHERE cus_id = C108)
答案 3 :(得分:0)
您可以根据自己的情况使用mysql DISTINCT 功能。
例如。 从table_name中选择distinct(column_name)WHERE cus_id<> 'C108'AND bus_id<> 'B109';
答案 4 :(得分:0)
尝试此查询
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')
答案 5 :(得分:0)
如果您的意思是忽略列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)