在SQL嵌套选择中找不到语法错误

时间:2019-05-03 17:23:05

标签: mysql sql

我的sql select语句中似乎找不到语法错误。盯着屏幕看了几个小时,什么也没得到。请帮助。

错误是#1064,它的语法错误是“在'EXISTS(SELECT e.item_Backid FROM Rental'附近'”)。

感谢您的帮助。

SELECT DISTINCT a.item_Backid, inv_name, item_size, item_Frontid, 
item_modeltype 
FROM Item a, Inventory c, Status b 
WHERE a.inv_id = c.inv_id and a.stat_id = b.stat_id and a.loc_id = 1 and 
     (a.stat_id = 1 or a.stat_id = 7) and 
      a.item_Backid NOT EXISTS (SELECT e.item_Backid 
                                FROM Rental d, Reserve1 e 
                                WHERE d.rent_id = e.rent_id and 
                                     (d.request_date = :cust_request_date or 
                                      d.request_date >= :cust_due_date or 
                                      d.due_date <= :cust_request_date))
ORDER BY inv_name, item_modeltype, item_Backid;

1 个答案:

答案 0 :(得分:0)

您的SQL不正确。

NOT EXISTS的格式为

SELECT <columns> FROM <table1>
 WHERE NOT EXISTS (SELECT <columns> FROM <table2>);

请参考以下链接进行说明 https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html

您的查询将与NOT IN

一起使用
SELECT DISTINCT a.item_Backid, inv_name, item_size, item_Frontid, 
item_modeltype 
FROM Item a, Inventory c, Status b 
WHERE a.inv_id = c.inv_id and a.stat_id = b.stat_id and a.loc_id = 1 and 
 (a.stat_id = 1 or a.stat_id = 7) and 
  a.item_Backid NOT IN (SELECT e.item_Backid 
                            FROM Rental d, Reserve1 e 
                            WHERE d.rent_id = e.rent_id and 
                                 (d.request_date = :cust_request_date or 
                                  d.request_date >= :cust_due_date or 
                                  d.due_date <= :cust_request_date))
ORDER BY inv_name, item_modeltype, item_Backid;