dream2891 2014-01-23 12:36
浏览 458

检查表mysql查询中可用的所有id值列表

table

id | catid | productid | 

1     3        100

2     244      110

3     400       300   

Hi, here is my query ,

select catid from table where catid NOT IN ('3','244','5')

i have a table looking like that above ,

1- what i need ,i have to check some list of catid values , if all the list catid is available an the table will return "yes" or some "rows".

2- Otherwise will return "no" all the list catid not available ?

I had tried but its useless ?

This is not correct one for my expecting result ?

SELECT catid , IF(catid IS NOT NULL , "Yes" , "No") FROM table where catid IN ('3','244','5');

what i need , the list of cat ids are "('3','244','5')" should be available an the table if once value missing an the table will return "No"

thanks for your help ?

  • 写回答

3条回答 默认 最新

  • doutuohan6606 2014-01-23 12:41
    关注

    You can do this in multiple steps

    For example : Your actual table where you need to check

    SELECT * FROM emp_old;
    +--------+-------+
    | emp_no | name  |
    +--------+-------+
    |      1 | abdul |
    |      2 | Jp    |
    |      3 | Ankur |
    +--------+-------+
    3 rows in set (0.00 sec)
    

    Insert your id's in a new table which you will check or pass in IN or NOT IN

    DROP TABLE IF EXISTS InsertIds;
    
    CREATE TABLE InsertIds(Id INT);
    
    INSERT INTO InsertIds VALUES(1),(2),(3),(4);
    
    SELECT * FROM InsertIds;
    +------+
    | Id   |
    +------+
    |    1 |
    |    2 |
    |    3 |
    |    4 |
    +------+
    4 rows in set (0.00 sec)
    

    Now try this one

    SELECT Id , IF(emp_no IS NOT NULL , "Yes" , "No") AS "Check status" FROM InsertIds a LEFT JOIN emp_old b ON a.Id = b.emp_no ;
    +------+--------------+
    | Id   | Check status |
    +------+--------------+
    |    1 | Yes          |
    |    2 | Yes          |
    |    3 | Yes          |
    |    4 | No           |
    +------+--------------+
    4 rows in set (0.00 sec)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值