Mysql 左外连接和连接表上的位置
为了详细说明,我从item和locations表中选择字段。连接是items表中的location_id和locations表中的id字段。加入后,我在locations表的city_文本字段上执行WHERE语句 自从我在第二张桌子上的什么地方做这件事以来,这是法律行动吗Mysql 左外连接和连接表上的位置,mysql,Mysql,为了详细说明,我从item和locations表中选择字段。连接是items表中的location_id和locations表中的id字段。加入后,我在locations表的city_文本字段上执行WHERE语句 自从我在第二张桌子上的什么地方做这件事以来,这是法律行动吗 SELECT uc_items.* , uc_users_store.id AS store_id, uc_users_store.store_name, uc_users_store
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
,则可能会导致意外结果。但是,您可以通过包含isnull
检查来捕获这些情况
比如说
WHERE (col = 'value' OR col IS NULL)
完全合法。但是,使用
内部联接
而不是左联接
更符合逻辑,因为您的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'
为什么不显示SQL?这是合法的,但是在
左联接
表上使用WHERE
条件可以有效地将其转换为内部联接
。发布您的表结构和查询,我们可以详细说明或帮助您完成使用SQL更新的解决方案,我以为描述会很清楚,对不起,你想要的结果是什么?您希望来自uc\U位置的行与$city
条件匹配还是不匹配?在这种情况下,您将使用(city\u文本,如“%$city%”或city\u文本为空)
,从而获得左连接的效果。看来有人在下面提到了这一点。。