【问题标题】:Select unique record from two tables when column count is not same in MySQL当 MySQL 中的列数不同时,从两个表中选择唯一记录
【发布时间】:2016-09-05 04:29:06
【问题描述】:

我有两张表,两张表都有相同的列,但一张表有额外的列。我需要根据列从它们中选择唯一记录,即下例中的 profilelink 存在于两个表中。

我尝试了以下方式的联合:

SELECT * FROM t1
UNION
SELECT * FROM t2

但我认为它是检查表中的所有列,然后取出结果,但在我的情况下,两个表中的列数不同,我需要仅基于一列获取记录。所以有人可以在这个查询中帮助我。

表结构如下

表 1

id  |   name    |   marks   |   profilelink
1   |   a       |   10      |   http://example.com/a
2   |   b       |   20      |   http://example.com/b
3   |   c       |   30      |   http://example.com/c
4   |   d       |   40      |   http://example.com/d

表 2

id  |   name    |   marks   |   Division    |   Result  |   profilelink
1   |   e       |   10      |   I           |   Pass    |   http://example.com/e
2   |   f       |   20      |   II          |   Pass    |   http://example.com/f
3   |   b       |   30      |   III         |   Pass    |   http://example.com/b
4   |   c       |   40      |   IV          |   Fail    |   http://example.com/c    

预期结果是

id  |   name    |   marks   |   Division    |   Result  |   profilelink
1   |   a       |   10      |   null        |   null    |   http://example.com/a
2   |   d       |   40      |   null        |   null    |   http://example.com/a
3   |   e       |   10      |   I           |   Pass    |   http://example.com/e
4   |   f       |   20      |   II          |   Fail    |   http://example.com/f

在上面的示例中,profilelink 在两个表中都很常见。结果顺序无关紧要。

【问题讨论】:

  • 您能添加表格结构和预期输出吗?
  • 列数是什么意思??
  • 一个很好的经验法则是“永远不要使用SELECT *
  • @Strawberry:谢谢你,但在这里我只是写了我的方法的例子。
  • @DanilaGanchar:我已经添加了表结构和预期结果。

标签: mysql


【解决方案1】:

好的。您可以选择每个表的特定列名称,但顺序相同,如下所示:

SELECT t1_id AS id
     , t1_name AS name 
  FROM t1 
 UNION 
SELECT t2_id 
     , t2_name 
  FROM t2

【讨论】:

    【解决方案2】:

    正如您所说的,您希望从两个表中获取不重复的记录。以下查询可能会对您有所帮助。

    SELECT id, name, marks, profilelink, count(id) as count FROM 
        (SELECT id, name, marks, profilelink 
                from table1 
        UNION 
            SELECT id, name, marks, profilelink from table2) A 
    GROUP BY profilelink where count = 1
    

    子查询将从两个表中获取记录并根据profileurl统计记录。

    【讨论】:

      【解决方案3】:

      我曾尝试使用此查询获取所有列作为结果:

       SELECT  `id`, `name`, `marks`, `Division`, `Result`, `profilelink`
            FROM table_b 
           UNION 
       SELECT  `id`, `name`, `marks`,'' as 'Division','' as 'Result', `profilelink`
            FROM table_a
      

      输出和示例:

      【讨论】:

      • 感谢 Gautam,但联合应仅基于 profilelink 列。不适合所有人。
      • 请试试这个查询:从(选择id,@9876543331@,@987645) @, Division, Result, profilelink, count(id) 作为计数来自 ( SELECT id, name, marks, Division, Result, profilelink FROM table_b UNION id, name, marks,null as 'Division',null as 'Result', profilelink FROM table_a )tbl group by name ) tbl_Val where count=1 ;
      【解决方案4】:

      请试试这个查询:

      SELECT `id`, `name`, `marks`, `Division`, `Result`, `profilelink` from (
      SELECT `id`, `name`, `marks`, `Division`, `Result`, `profilelink`,  count(id) as count from (
      SELECT  `id`, `name`, `marks`, `Division`, `Result`, `profilelink`
        FROM table_b 
       UNION 
      SELECT  `id`, `name`, `marks`,null as 'Division',null as 'Result', `profilelink`
        FROM table_a )tbl  group by name )  tbl_Val where  count=1  ;
      

      【讨论】:

      • 我认为我们需要使用 profilelink 列在两个表中进行比较,因为在两个表中只有 profilelink 列可以识别重复。我说的对吗?
      • 好的,老板,这只是一个获得结果的想法,请您可以随意进行。
      • SELECT id, name, marks, Division, Result, profilelink from (SELECT id, name, @9876543330@, Division, , Result, profilelink, count(id) as count from ( SELECT id, name, marks, Division, Result, profilelink FROM table_b UNION SELECT @98765434@ 987654341@, marks,null as 'Division',null as 'Result', profilelink FROM table_a )tbl group by profilelink ) tbl_Val where count=1 ;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2016-10-25
      • 2021-12-08
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多