English 中文(简体)
• 如何通过选举考试加入两表
原标题:How to join two tables in mysql via SELECT
  • 时间:2011-07-06 21:33:49
  •  标签:
  • mysql

因此,我有两个表格。 两者共有五个栏目(单位名称、用户数、用户界限、专题、额外),同时各栏也有其他栏目。

我想要的是使用特别选举和检索表A+表组合。 因此,房名价值必须具有独特性。

表3 A中载有一个房间名称=1室,表B载有一个房间名称=1室,优于表A,不从表B中增列该项目。 考虑到只有房间名称价值不变。 用户数、用户界限、对象、外差数与表A不同。 B 即使房间名称价值相同。

最佳回答
SELECT roomname, usercount, userlimit, topic, extra
FROM TableA

UNION ALL

SELECT roomname, usercount, userlimit, topic, extra
FROM TableB
WHERE roomname NOT IN
    ( SELECT roomname FROM TableA )
问题回答

你们试图做的比照更为完善。 前者只是给你一个综合成果,而后者则发挥JOQIN的作用,改变结果。

To combine the result of two queries, UNION the SELECT queries, such as

(SELECT roomname, usercount, userlimit, topic, extra
    FROM TableA)
UNION DISTINCT
(SELECT roomname, usercount, userlimit, topic, extra
    FROM TableB);

<编码> UNION DISTINCT 消除了重复。 我没有一台自动服务器来测试yn子,但是,在manual上,应当正确。

SELECT roomname, usercount, userlimit, topic, extra
FROM
     (
(SELECT roomname, usercount, userlimit, topic, extra
    FROM TableA)
UNION
(SELECT roomname, usercount, userlimit, topic, extra
    FROM TableB)
) AAA
GROUP BY roomname

通知 我不使用任何合并的职能,这只会使质询只反映每个群体的第一个结果(在这种情况下,这些结果可能来自联合国决定中的第一个选举。

Also @ypercube solution s good (and simpler to understand than mine). But due to a bug in MySQl, I think my solution is more efficient (you need to check this).

SELECT roomname, usercount, userlimit, topic, extra
FROM TableA

UNION

SELECT roomname, usercount, userlimit, topic, extra
FROM TableA

否则,MySQL的工会问询是不同的,消除了重复浏览(aka UNION DISTINCT)。 如果您希望获得重复浏览量,则请填写UNION ALL


ok,因为你现在说,只有房间名称在桌子之间是常见的/复制的,所以如何做这样的事情:

select tableA.*, tableB.*
from tableA
join tableB on tableA.roomname = tableB.roomname
where (tableA.usercount <> tableB.usercount) or (tableA.userlimit <> tableB.userlimit)
   or (tableA.topic <> tableB.topic) or (tableA.extra <> tableB.extra)

这使两个表格中所有各行各有相同的房间名称,但在另一个或多个领域有差异。 如果你只想显示这些领域的确切重复,则将<代码><>改为=


第2部分

select tableB.*
from tableB
left join tableA on tableA.roomname = tableB.roomname
where tableA.roomname is null

这将使所有记录从表B中收回,在表A中,房名外地有对应关系。



在文件后解释:

SELECT roomname, usercount, userlimit, topic, extra
FROM TableA

UNION ALL

SELECT roomname, usercount, userlimit, topic, extra
FROM TableB
WHERE roomname NOT IN
    ( SELECT roomname FROM TableA )




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...