2011-10-19 231 views
-3

Possible Duplicate:
Combining several database table together?結合兩個表與SQL JOIN?

我從兩個數據庫表中要獲取輸出是這樣的:

One-week tour of Istanbul_1 | 88888 & 99999 $ 112233 $ 445566 | Three nights and two days | 15:29
One-week tour of Istanbul_2 | 55555 & 66666 $ 77777 | Three nights and two days | 12:03
One-week tour of Istanbul_3 | 11111 & 22222 $ 33333 $ 44444 | Three nights and two days | 12:03

這些都是我的表:

enter image description here enter image description here

更新:

它給我這個輸出:http://img708.imageshack.us/img708/2404/outputnz.gif,但如果我把$查找值「55555」,其中輸出爲:http://img528.imageshack.us/img528/3576/outputnow.gif但我想要的是:http://img832.imageshack.us/img832/120/outputwant.gif。怎麼樣?

$query = $this -> db -> query(' 
    SELECT 
     @rownum := @rownum + 1 rownum, 
     tour_foreign.id, 
     tour_foreign.name, 
     MIN(tour_foreign_residence.name_re) AS name_re, 
     tour_foreign.service, 
     tour_foreign.date_go, 
     tour_foreign.date_back, 
     tour_foreign.term, 
     tour_foreign.useradmin_submit, 
     tour_foreign.date_submit, 
     GROUP_CONCAT(tour_foreign_residence.name_re 
        ORDER BY tour_foreign_residence.name_re 
        SEPARATOR " $ " 
        ) AS name_re_all 
    FROM tour_foreign 
     INNER JOIN tour_foreign_residence 
     ON (tour_foreign.id = tour_foreign_residence.relation) 
     JOIN (SELECT @rownum := 0) r 
    WHERE tour_foreign.name LIKE "%' . $find . '%" 
     OR tour_foreign_residence.name_re_all LIKE "%' . $find . '%" 
    GROUP BY tour_foreign.id '); 

我從上面的SQL以下錯誤得到:

A Database Error Occurred
Error Number: 1054

Unknown column 'tour_foreign_residence.name_re_all' in 'where clause'

SELECT @rownum := @rownum + 1 rownum, tour_foreign.id, tour_foreign.name, MIN(tour_foreign_residence.name_re) AS name_re, tour_foreign.service, tour_foreign.date_go, tour_foreign.date_back, tour_foreign.term, tour_foreign.useradmin_submit, tour_foreign.date_submit, GROUP_CONCAT(tour_foreign_residence.name_re ORDER BY tour_foreign_residence.name_re SEPARATOR " $ ") AS name_re_all FROM tour_foreign INNER JOIN tour_foreign_residence ON ( tour_foreign.id = tour_foreign_residence.relation) JOIN (SELECT @rownum := 0) r WHERE tour_foreign.name LIKE "%%" OR tour_foreign_residence.name_re_all LIKE "%%" GROUP BY tour_foreign.id

Filename: D:\xampp\htdocs\system\database\DB_driver.php

Line Number: 330

回答

0

你需要一羣被什麼東西或總GROUP_CONCAT()將收集所有的行成一個:

GROUP BY tour_foreign.id 

用途:

$query = $this -> db -> query(' 
    SELECT 
     @rownum := @rownum + 1 rownum, 
     tour_foreign.id, 
     tour_foreign.name, 
     MIN(tour_foreign_residence.name_re) AS name_re, 
     tour_foreign.service, 
     tour_foreign.date_go, 
     tour_foreign.date_back, 
     tour_foreign.term, 
     tour_foreign.useradmin_submit, 
     tour_foreign.date_submit, 
     GROUP_CONCAT(tour_foreign_residence.name_re 
        ORDER BY tour_foreign_residence.name_re 
        SEPARATOR " $ " 
        ) AS name_re_all 
    FROM tour_foreign 
     INNER JOIN tour_foreign_residence 
     ON (tour_foreign.id = tour_foreign_residence.relation) 
     JOIN (SELECT @rownum := 0) r 
    WHERE tour_foreign.name LIKE "%' . $find . '%" 
     OR tour_foreign_residence.name_re LIKE "%' . $find . '%" 
    GROUP BY tour_foreign.id '); 
+0

請看這裏:http://pastebin.com/DJndg1Et name_re只有1沒有全部!? –

+0

'name_re'應該得到每個組的第一個id?或者是什麼? –

+0

name_re is here:'tour_foreign_residence.name_re,'在我的sql代碼中的第一篇文章。你看見了嗎? –

0

嘗試在SQL語句的末尾添加GROUP BY子句。

+0

我使用代碼@ypercube。看到這裏:http://pastebin.com/DJndg1Et name_re只有1沒有全部!? –

0
SELECT a.name, GROUP_CONCAT(b.name_re ORDER BY b.name_re SEPARATOR " & "), a.term, a.time_go 
FROM tour_foreign a INNER JOIN tour_foreign residence b 
ON a.id = b.relation 
GROUP BY a.name 

我只是在學習SQL,所以我把你的問題作爲一個練習。希望它有效。

+0

我使用代碼@ypercube。請看這裏:http://pastebin.com/DJndg1Et name_re只有1個不是全部!?(應該如何使用你的代碼'a'&'b'?) –

+0

a和b是tour_foreign和tour_foreign_residence的別名。 – Lonsor