我希望从两个数据库表得到这样的输出:
伊斯坦布尔的一周之旅_1 88888& 99999 $ 112233 $ 445566 |三晚两天| 15:29
伊斯坦布尔2周的一周之旅55555& 66666 $ 77777 |三晚两天| 12:03
伊斯坦布尔3周的一周之旅11111& 22222 $ 33333 $ 44444 |三晚两天| 12:03
这些是我的表格:
更新
它给了我这个输出:http://img708.imageshack.us/img708/2404/outputnz.gif但是如果我输入$ find值“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上面得到以下错误:
发生数据库错误
错误编号:1054
'where'中的未知列'tour_foreign_residence.name_re_all' 条款“
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 订购者:tour_foreign_residence.name_re分隔符“$”)AS name_re_all FROM tour_foreign INNER JOIN tour_foreign_residence ON( tour_foreign.id = tour_foreign_residence.relation)加入(选择 @rownum:= 0)r WHERE tour_foreign.name LIKE“%%”OR tour_foreign_residence.name_re_all LIKE“%%”GROUP BY tour_foreign.id
文件名: d:\ XAMPP \ htdocs中\ SYSTEM \数据库\ DB_driver.php
行号:330
答案 0 :(得分: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 ');
答案 1 :(得分:0)
尝试在SQL语句的末尾添加GROUP BY
子句。
答案 2 :(得分: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,所以我把你的问题当作练习。希望它有效。