尝试投影有效的数据库: 我们看广告,卖家发布他们的电话号码,卖家可以有多个电话号码,卖家可以用不同的方式发布他们的手机...... 简单的图像清楚地了解: 我想知道所有广告,手机和目标卖家只知道一部手机[phone_1 | phone_2 | ... | phone_4]!
这是我想要使用的表结构:
你可以注意到的地方:
一切都很好,但我怎么能用一个查询“选择手机,adver_num WHERE my_phone IN(head& tail_phones)”?
我相信,自我加入会帮助我,但我现在使用它的方式不太合适:
SELECT a1.`advert_site_num`,
a1.`head_phone`
FROM `test_table` a1
LEFT JOIN `test_table` a2 ON a1.`tail_phones` = a2.`head_phone`
UNION
SELECT a3.`advert_site_num`,
a3.`tail_phones`
FROM `test_table` a3
LEFT JOIN `test_table` a4 ON a3.`tail_phones` = a4.`head_phone`
ORDER BY `advert_site_num`, `head_phone`;
返回通过电话链接的所有记录,不过滤搜索电话
答案 0 :(得分:0)
你为什么不在哪里过滤?我不确定你理解你的问题:
SELECT `advert_site_num`, GROUP_CONCAT(`phone` SEPARATOR ',') FROM (
SELECT `advert_site_num`,
`tail_phones` AS `phone`
FROM `test_table`
WHERE a1.`tail_phones` = myphone
UNION
SELECT `advert_site_num`,
`head_phone` AS `phone`
FROM `test_table`
WHERE a1.`head_phone` = myphone
)
GROUP BY `advert_site_num`;