如何在mysql中显示多个表列表
我有两张桌子
1)Sales_media 2)Rent_media
1)sales_media table
ID | Property_titile | Username| city|
1 | Big House Sale | xyz | newyork
2) rent_media table
ID | Property_titile | Username| city|
1 | Big House Rent | xyz | newyork
我想显示两个表结果togetter 喜欢这个
1 | Big House Sale | xyz | newyork
1 | Big House Rent | xyz | newyork
我该怎么办? 请帮我解决这个问题
感谢
这是代码,但它没有显示结果
SELECT * FROM sales_media,rent_media WHERE $ construct AND user_name ='$ row-> user_name'ORDER BY city DESC,ID DESC
答案 0 :(得分:1)
请使用这个:
SELECT * FROM sales_media inner join rent_media on sales_media.username = rent_media.username ORDER BY city DESC, ID DESC
答案 1 :(得分:0)
假设两个表上的列名相同。
(SELECT * FROM sales_media WHERE user_name='$row->user_name')
UNION
(SELECT * FROM rent_media WHERE user_name='$row->user_name')
ORDER BY city DESC, ID DESC
答案 2 :(得分:0)
您可以尝试以下查询:
SELECT ID, Property_titile, Username, city FROM sales_media table
UNION ALL
SELECT ID, Property_titile, Username, city FROM rent_media table ORDER BY city DESC;