Mysql 从两个表中选择位置
我是SQL新手,在编写以下查询时遇到困难 情景 用户有两个地址,家庭地址App\user和列表地址App\listing。当访问者搜索某个郊区、邮政编码或州的列表时,如果用户的列表地址不匹配(但如果家庭地址匹配),则它们也将出现在搜索结果中 例如:如果一个访问者搜索墨尔本,我想包括来自墨尔本的列表以及在墨尔本有地址的用户的列表 预期产出: 桌子 用户:Mysql 从两个表中选择位置,mysql,sql,Mysql,Sql,我是SQL新手,在编写以下查询时遇到困难 情景 用户有两个地址,家庭地址App\user和列表地址App\listing。当访问者搜索某个郊区、邮政编码或州的列表时,如果用户的列表地址不匹配(但如果家庭地址匹配),则它们也将出现在搜索结果中 例如:如果一个访问者搜索墨尔本,我想包括来自墨尔本的列表以及在墨尔本有地址的用户的列表 预期产出: 桌子 用户: id first_name email 1 Mathew mathew.afsd@gmail.com 2 Zammy
id first_name email
1 Mathew mathew.afsd@gmail.com
2 Zammy Zamm@xyz.com
3 Tammy tammy@unknown.com
4 Foo foo@hotmail.com
5 Bar bar@jhondoe.com.au
清单:
id user_id hourly_rate description
1 1 30 ABC
2 2 40 CBD
3 3 50 XYZ
4 4 49 EFG
5 5 10 Efd
地址:
id addressable_id addressable_type post_code suburb state latitude longitude
3584 1 App\\User 2155 Rouse Hill NSW -33.6918372 150.9007221
3585 2 App\\User 3000 Melbourne VIC -33.6918372 150.9007221
3586 3 App\\User 2000 Sydney NSW -33.883123 151.245969
3587 4 App\\User 2008 Chippendale NSW -33.8876392 151.2011224
3588 5 App\\User 2205 Wolli Creek NSW -33.935259 151.156301
3591 1 App\\Listing 3000 Melbourne VIC -37.773923 145.12385
3592 2 App\\Listing 2030 Vaucluse NSW -33.858935 151.2784079
3597 3 App\\Listing 4000 Brisbane QLD -27.4709331 153.0235024
3599 4 App\\Listing 2000 Sydney NSW -33.91741 151.231307
3608 5 App\\Listing 2155 Rouse Hill NSW -33.863464 151.271504
试试这个
SELECT
a.addressable_id AS `userid`,
b.first_name AS `username`
FROM
addresses AS a JOIN users AS b ON a.addressable_id=b.id
WHERE
a.suburb = 'Melbourne';
if < addressable_id > has relation with < id > in listing table,
SELECT
a.addressable_id AS `userid`,
b.first_name AS `username`
FROM
addresses AS a JOIN users AS b ON a.addressable_id=b.id AND addressable_type='App\\User'
WHERE
a.suburb = 'Melbourne'
UNION
SELECT
b.user_id AS `userid`,
c.first_name AS `username`
FROM
addresses AS a JOIN listings AS b ON a.addressable_id=b.id AND addressable_type='App\\Listing'
JOIN users AS c ON b.user_id=c.id
WHERE
a.suburb = 'Melbourne';
试试这个
SELECT
a.addressable_id AS `userid`,
b.first_name AS `username`
FROM
addresses AS a JOIN users AS b ON a.addressable_id=b.id
WHERE
a.suburb = 'Melbourne';
if < addressable_id > has relation with < id > in listing table,
SELECT
a.addressable_id AS `userid`,
b.first_name AS `username`
FROM
addresses AS a JOIN users AS b ON a.addressable_id=b.id AND addressable_type='App\\User'
WHERE
a.suburb = 'Melbourne'
UNION
SELECT
b.user_id AS `userid`,
c.first_name AS `username`
FROM
addresses AS a JOIN listings AS b ON a.addressable_id=b.id AND addressable_type='App\\Listing'
JOIN users AS c ON b.user_id=c.id
WHERE
a.suburb = 'Melbourne';
根据我对您问题的理解,对于访客提供的任何郊区,您希望包括用户地址与提供的郊区相同或列表地址与提供的郊区相同的所有列表 假设addressable_id列与Users表和Listings表的id相关,根据addressable_type列中的值,您可以使用以下查询联接并获得所需的结果:
Select l.*
From Listings l
inner join Addresses a on ((a.addressable_id = l.user_Id and a.addressable_type = 'App\\User') or (a.addressable_id = l.Id and a.addressable_type = 'App\\Listings'))
inner join Addresses a1 On a1.addressable_id = a.addressable_id and a1.Suburb = 'Melbourne'
根据我对您问题的理解,对于访客提供的任何郊区,您希望包括用户地址与提供的郊区相同或列表地址与提供的郊区相同的所有列表 假设addressable_id列与Users表和Listings表的id相关,根据addressable_type列中的值,您可以使用以下查询联接并获得所需的结果:
Select l.*
From Listings l
inner join Addresses a on ((a.addressable_id = l.user_Id and a.addressable_type = 'App\\User') or (a.addressable_id = l.Id and a.addressable_type = 'App\\Listings'))
inner join Addresses a1 On a1.addressable_id = a.addressable_id and a1.Suburb = 'Melbourne'
试试这个。你可以查一下。
试试这个。你可以查一下。
你可以使用join或union。看看这是否有帮助:你可以发布你想要的输出,这会更容易理解。你可以使用join或union。看看这是否有帮助:您是否也可以发布所需的输出,这将更容易理解我如何选择纬度和经度作为返回列表的查询?我如何选择纬度和经度作为返回列表的查询?如果您的表很大并且有适当的索引,我想您应该尽可能使用内部联接以获得更好的性能。不管怎样,你已经得到了答案,所以不用担心。。有关内部联接和左联接的详细信息,请查看以下链接。。如果您的表很大并且有适当的索引,我想您应该尽可能使用内部联接以获得更好的性能。不管怎样,你已经得到了答案,所以不用担心。。有关内部联接和左联接的详细信息,请查看以下链接。。