我不确定如何编写符合这些条件的查询:
SELECT
gems.*, metals.*
FROM
`locations`
JOIN
gems ON locations.locatable_id = gems.id AND locations.locatable_type = 'Gem'
JOIN
metals ON locations.locatable_id = metals.id AND locations.locatable_type = 'Metal'
WHERE
locations.latitude BETWEEN 0.0000 AND 1.0000 AND locations.longitude BETWEEN -1.0000 AND -2.0000
如果WHERE语句中的关联gems
结果是locatable_type locations
,则结果将选择一些Gem
,如果metals
,则选择SELECT locations
属于Metal
类型。
是否有可能产生这样的混合结果?
答案 0 :(得分:0)
您需要JOIN
locations
表两次,因为locations.locatable_type
不能同时是'Gem'和'Metal',例如:
SELECT
gems.*, metals.*
FROM
`locations` l1
JOIN
gems ON l1.locatable_id = gems.id AND l1.locatable_type = 'Gem'
JOIN
users ON locations.locatable_id = metals.id AND locations.locatable_type = 'Metal'
WHERE
l1.latitude BETWEEN 0.0000 AND 1.0000 AND l1.longitude BETWEEN -1.0000 AND -2.0000
UNION
SELECT
gems.*, metals.*
FROM
`locations` l=2
JOIN
users ON l2.locatable_id = metals.id AND l2.locatable_type = 'Metal'
WHERE
l2.latitude BETWEEN 0.0000 AND 1.0000 AND l2.longitude BETWEEN -1.0000 AND -2.0000
答案 1 :(得分:0)
您只需要重新构建一下查询。以下应该有效...
currency price new_price
SGD 100 72.99
USD 80 80
EUR 75 65.22