简体   繁体   English

MYSQL选择使用concat与不在陈述

[英]MYSQL select using concat with not in statment

can someone correct me on this syntax error. 有人可以纠正我这个语法错误。 am trying to search users by firstname, lastname and their email 正在尝试通过名字,姓氏和他们的电子邮件搜索用户

SELECT
  USERID,
  FNAME,
  LNAME
FROM
  USERS
WHERE
  CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE('keyword ', ' ', '%'), '%', '%') NOT IN (
    SELECT
      PRODUCT_ID
    FROM
      ITEMS
    WHERE
      USER = 'JANE'
      AND PRICE = '400'
      AND ORDER = 'PAID'
  )

i want to search and get userid,firstname and lastname. 我想搜索并获取用户名,名字和姓氏。 but i dont want to get jane, price = 400 and order = paid 但我不想得到简,价格= 400,订单=已付款

TABLE USER

USERID        FNAME     LNAME          EMAIL 
1             JANE      DEO          TES@TED.COM
2              KEL      DEO          TES@TED.COM
3              MK       DEO          TES@TED.COM


TABLE ITEMS 

PRODUCT_ID  PRICE    ITEM_NUM  USER    USERID
1           400        40      JANE       1
2           200        20      KEL        2
3           100        10      MK         3

so i want to get the rest people apart from JANE ROW 所以我想让其余的人与简·罗

AND MY TRY IS 我的尝试是

SELECT SND.USERID, SND.FNAME, SND.LNAME WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE('keyword', ' ', '%'), '%', '%') FROM items as M JOIN users as SND ON SND.USERID WHERE NOT EXISTS(SELECT * FROM ITEMS WHERE M.USER = 'JANE' AND M.PRICE ='400' AND M.ITEM_NUM ='40')

You cannot include a where clause between the select and the from clauses 您不能在select和from子句之间包含where子句

SELECT
   SND.USERID
 , SND.FNAME
 , SND.LNAME

>> no where clause here

FROM items AS M
INNER JOIN users AS SND ON SND.USERID
WHERE NOT EXISTS (
  SELECT *
  FROM ITEMS
  WHERE M.USER = 'JANE'
   AND M.PRICE = '400'
   AND M.ITEM_NUM = '40'
  )

Just include all the wanted conditions int the one (and only) where clause, combine those using AND's or OR's as needed. 只需将所有需要的条件包括在一个(唯一)where子句中,并根据需要使用AND或OR合并那些条件即可。 I have changed the join condition, and altered the NOT syntax used also. 我更改了联接条件,并更改了所使用的NOT语法。

SELECT
   SND.USERID
 , SND.FNAME
 , SND.LNAME
FROM items AS M
INNER JOIN users AS SND ON M.userid = SND.userid
WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE('keyword', ' ', '%'), '%', '%') 
AND NOT (
       M.USER = 'JANE'
   AND M.PRICE = '400'
   AND M.ITEM_NUM = '40'
  )

I'm not that comfortable with your use of concat() in the above however, it looks very inefficient, instead why not just repeat the LIKE tests on each column. 我对上面使用concat()感到不满意,但是它看起来效率很低,相反,为什么不对每列重复LIKE测试。 (Yes it may make the SQL code longer, but that is not an indicator of the execution time of the query.) (是的,这可能会使SQL代码更长,但这并不表示查询的执行时间。)

SELECT
   SND.USERID
 , SND.FNAME
 , SND.LNAME
FROM items AS M
INNER JOIN users AS SND ON M.userid = SND.userid
WHERE (
   SND.FNAME LIKE CONCAT('%', REPLACE('keyword', ' ', '%'))
or SND.LNAME LIKE CONCAT('%', REPLACE('keyword', ' ', '%'))
or SND.EMAIL LIKE CONCAT('%', REPLACE('keyword', ' ', '%'))
)
AND NOT (
       M.USER = 'JANE'
   AND M.PRICE = '400'
   AND M.ITEM_NUM = '40'
  )

Note the use of indents and parentheses when using or in a where clause. 请注意在where子句中或其中使用缩进和括号。 (You do not have to place SQL into one row, not sure why you did that in the question.) (您不必将SQL放在一行中,不必确定为什么要在问题中这样做。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM