簡體   English   中英

左和右在連接台上的位置

[英]LEFT OUTER JOIN AND WHERE on joining table

詳細地說,我從項目和位置表中選擇字段。 連接是項目表中的location_id和位置表中的id字段。 加入后,我在locations表中對city_text字段執行WHERE語句。

自從我在第二張桌子上進行實地調查以來,這是法律訴訟嗎?

SELECT uc_items.* ,
       uc_users_store.id AS store_id,
       uc_users_store.store_name,
       uc_users_store.address,
       uc_users_store.work_hours,
       uc_locations.city_text AS city,
       uc_locations.zipcode_text AS zipcode,
       uc_locations.state_text AS STATE,
       uc_locations.country_text AS country
FROM uc_items
LEFT OUTER JOIN uc_users_store ON uc_items.store_id=uc_users_store.id
LEFT OUTER JOIN uc_locations ON uc_users_store.store_location_id=uc_locations.id
WHERE uc_locations.city_text LIKE "%'.$city.'%"
  AND uc_items.iname LIKE "%'.$description.'%"
  AND uc_items.expiration_stamp > '.time().'
ORDER BY uc_items.posting_stamp DESC,
         uc_items.discount DESC

這是合法的,但是如果測試值為NULL ,可能會導致意外結果。 但是,您可以通過包含IS NULL檢查來捕獲這些情況。

例如

WHERE (col = 'value' OR col IS NULL)

完全合法。 但是,使用INNER JOIN而不是LEFT JOIN會更加合乎邏輯,因為您的WHERE語句與要連接的表有關。 偽示例:

SELECT t1.something, t2.somethin FROM first_table t1 INNER JOIN second_table t2 ON t1.some_id = t2.some_id_from_t1 WHERE t2.some_column='something'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM