duanniesui6391 2015-05-25 14:53
浏览 89
已采纳

即使在变量列表中的列中至少有一个值匹配时,Sql查询也可以选择行计数

I have a table name called Kits in which has 5 columns for Book ISBN.

  • So i want to select Row even when at least 1 value of ISBN gets matched with any of the 5 columns(ISBN_BOOK1,ISBN_BOOK2,ISBN_BOOK3,ISBN_BOOK4,ISBN_BOOK5) of table kits meant for Storing ISBN.
  • if any one of the ISBN exists then return count.

I have written an sql query for this but I am getting row output only if last value is matching with any row. In my case the value is 7589765432 Here is my sql query:

Select * 
  from kits k
  left 
  join kits_cstm kc
    on k.id = kc.id_c 
 where '8192933563' 
   and '8192933567' 
   and '8192933568' 
   and '8192933564' 
   and '7589765432' IN (kc.isbn_book_1_c,kc.isbn_book_2_c,kc.isbn_book_3_c,kc.isbn_book_4_c,kc.isbn_book_5_c) 
   and kc.city_c = 'BAN' 
   and kc.kit_for_c = 'SCHL' 
   and k.deleted = 0 
 ORDER 
    BY k.kit_id DESC 
  • 写回答

3条回答 默认 最新

  • dsieyx2015 2015-05-25 15:18
    关注

    You can't use "AND" to conjoin values to one operator

    WRONG WAY: WHERE '8192933536' AND '8192933567' IN ({array]) RIGHT WAY: WHERE '8192933536' IN ({array]) AND '8192933567' IN ({array])

    But I suspect that what you're actually trying to do is more like:

    SELECT 
          * 
    FROM 
         kits k LEFT JOIN kits_cstm kc
              ON k.id = kc.id_c 
    WHERE 
         ('8192933563' IN (kc.isbn_book_1_c,kc.isbn_book_2_c,kc.isbn_book_3_c,kc.isbn_book_4_c,kc.isbn_book_5_c)
          OR 
         '8192933567' IN (kc.isbn_book_1_c,kc.isbn_book_2_c,kc.isbn_book_3_c,kc.isbn_book_4_c,kc.isbn_book_5_c)
          OR 
         '8192933568' IN (kc.isbn_book_1_c,kc.isbn_book_2_c,kc.isbn_book_3_c,kc.isbn_book_4_c,kc.isbn_book_5_c)
          OR 
         '8192933564' IN (kc.isbn_book_1_c,kc.isbn_book_2_c,kc.isbn_book_3_c,kc.isbn_book_4_c,kc.isbn_book_5_c)
          OR 
         '7589765432' IN (kc.isbn_book_1_c,kc.isbn_book_2_c,kc.isbn_book_3_c,kc.isbn_book_4_c,kc.isbn_book_5_c) 
          )
    AND
         kc.city_c = 'BAN' 
    AND
         kc.kit_for_c = 'SCHL' 
    AND
         k.deleted = 0 
    ORDER BY 
         k.kit_id DESC 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里