用sql JOIN合并两个表?

发布于 2024-12-11 00:36:50 字数 2916 浏览 0 评论 0原文

可能的重复:
将多个数据库表组合在一起?

我想从两个数据库表中获得如下输出:

伊斯坦布尔一周游_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 ORDER BYtour_foreign_residence.name_re SEPARATOR " $ " ) AS name_re_all FROMtour_foreign INNER JOINtour_foreign_residence ON ( tour_foreign.id =tour_foreign_residence.relation ) JOIN (选择 @rownum := 0) r WHEREtour_foreign.name LIKE "%%" OR tour_foreign_residence.name_re_all LIKE "%%" 分组依据 tour_foreign.id

文件名: D:\xampp\htdocs\system\database\DB_driver.php

行号:330

Possible Duplicate:
Combining several database table together?

I want from two database table get output like this:

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

These are my tables:

enter image description here
enter image description here

Update:

it give me this output: http://img708.imageshack.us/img708/2404/outputnz.gif but if i put in $find value "55555" for where output is as: http://img528.imageshack.us/img528/3576/outputnow.gif but i want out is as: http://img832.imageshack.us/img832/120/outputwant.gif . How is it?

$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 ');

I get from above sql following error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-12-18 00:36:50

您需要按某些内容进行分组,否则聚合 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 ');

You need to group by something or the aggregate GROUP_CONCAT() will collect all rows into one:

GROUP BY  tour_foreign.id 

Use:

$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 ');
失与倦" 2024-12-18 00:36:50

尝试在 SQL 语句末尾添加 GROUP BY 子句。

Try adding a GROUP BY clause at the end of the SQL statement.

甜嗑 2024-12-18 00:36:50
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,所以我把你的问题作为练习。希望它有效。

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

I'm just learning SQL so i picked up your problem as an exercise. Hope it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文