0
我一直在搜索嵌套查詢,但無法找到任何我可以掌握的關於如何去做這個特定操作的東西。查詢關閉查詢MYSQL
首先,我會告訴你我的DB模式
CREATE TABLE slsemp
( empID char(4) NOT NULL,
empname varchar(50) NOT NULL,
prtime enum('yes','no') NOT NULL, # we can only accept yes or no values to the part-time employee indicator
RegionID char(2) NOT NULL, # enums are often used for boolean values in a BD
PosID char(4) NOT NULL,
PRIMARY KEY (empID),
FOREIGN KEY (regionID) REFERENCES region (RegionID),
FOREIGN KEY (PosID) REFERENCES slspos(PosID));
# create the sales transactions table
CREATE TABLE slstranx
( tranxID int(10) NOT NULL AUTO_INCREMENT, #starts at a certain number, then increments accordingly
empID char(4) NOT NULL,
ProdID char(3) NOT NULL,
Qty int(5) NOT NULL,
Unitprice Decimal(5,2) NOT NULL, # please note we are capturing the unit price at the transactional level in this case
SAmt Float(10,2), #store up to 10 (TOTAL?) with 2 digits past decimal point
SalesDate date, # 'date' data type is organized as follows YYYYMMDD. You need to make sure that raw data contains the DATE in the YYYYMMDD format
# For example 20150915
PRIMARY KEY (tranxID),
FOREIGN KEY (ProdID) REFERENCES product (ProdID),
FOREIGN KEY (empID) REFERENCES slsemp (empID));
現在,我想找到那些在沒有作出任何銷售西部地區的員工。我想我會通過這兩個表之間的左外連接來完成此操作,然後根據空tranx ID查詢結果表。我知道了大部分的方式出現,這是我的查詢:
SELECT e.empID, t.tranxID, e.RegionID
FROM slsemp e LEFT OUTER JOIN slstranx t ON e.empID=t.empID
WHERE e.RegionID='WS'
我的問題是,如何查詢基於這個結果表的標準。如果我能做到這一點,我只需要選擇slstranxID = null的條件。
我明白了。我以類似的方式得到了我的結果,除了我使用slstranx.tranxID爲空。結果表是相同的。 我想最後的問題是:slstranx有什麼區別。是null和slstranx。 = null? –
@ MadisonC.Brewer在sql中sintax some_column = null是錯誤的(sintax錯誤)你必須使用some_column爲null,或者你可以用例如:ifnull(some_column,'')=''覆蓋rhe null值。 ://dev.mysql.com/doc/refman/5.7/en/working-with-null.html – scaisEdge
奇數,作爲結果查詢執行,它只是導致一個空表。好吧。現在我明白了。 –