检查同一列mysql查询中的不同数据值?

时间:2013-08-24 05:15:17

标签: mysql database mysqli database-connection relational-database

id_detail_item id_item   id_detail_item_name
1              1         abc 
2              1         abcd
3              1         cde
4              3         zki
5              3         zkr

如何检查同一“id_item”中“id_detail_item_name”中是否有两个不同的数据?

我尝试了以下内容,但收到了错误:

SELECT id_item FROM table_detail_item WHERE id_detail_item_name='abc' AND id_detail_item_name='abcd';

3 个答案:

答案 0 :(得分:0)

那是因为AND运算符意味着必须满足两个条件。

尝试:

SELECT * FROM table_detail_item 
WHERE id_item = '1' 
AND id_detail_item_name IN ('abc', 'abcd');

答案 1 :(得分:0)

这应列出id_detail_item_name

至少有两个不同id_item的所有条目
SELECT id_item, MIN(id_detail_item_name) nam1,max(id_detail_item_name) nam2 
FROM table_detail_item 
GROUP by id_item HAVING MIN(id_detail_item_name) nam1<MAX(id_detail_item_name)

答案 2 :(得分:0)

select id_item
  from (
              select min(id_detail_item_name) mmin
                   , max(id_detail_item_name) mmax
                from table_detail_item
            group by id_item
       ) agg
 where agg.mmin <> agg.mmax
     ;