2016-04-16 21 views
1

顯示多個表列表如何在mysql中顯示多個表列表如何在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 

我想顯示兩個table result togetter 這樣的

1 | Big House Sale | xyz  | newyork 
1 | Big House Rent | xyz  | newyork 

我怎麼能做到這一點 請幫我解決這個問題

感謝

這裏的代碼,但它沒有顯示結果

SELECT * FROM sales_media,rent_media其中$建設和USER_NAME ='$按行> USER_NAME」 ORDER BY城市DESC,ID DESC

+0

是什麼'$ construct'的價值? –

+0

是什麼答案似乎有用然後請標記爲正確的。 – naf4me

回答

0

兩個TABL假設列名es是一樣的。

(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 
1

請使用一個:

SELECT * FROM sales_media inner join rent_media on sales_media.username = rent_media.username ORDER BY city DESC, ID DESC 
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;