【问题标题】:MySQL - 3 different tables with the same column join in 1 columnMySQL - 具有相同列的 3 个不同表加入 1 列
【发布时间】:2013-05-26 07:10:58
【问题描述】:

所以我在查询时尝试将 3 个表加入 1 列。

三个表是

investigators.name, ancientones.name,monsters.name.

我用来回调所有名字的查询是

SELECT investigators.name ,ancientones.name ,monsters.name FROM investigators, ancientones, monsters WHERE ((investigators.name LIKE'a%') OR (ancientones.name LIKE'a%')OR (monsters.name LIKE'a%'))

但这会返回 3 个表行,其中“名称”包含表 1、2 和 3 中的值。 我想要一个列中的所有值,但我没有那么多 SQL 经验..

【问题讨论】:

  • 奇怪的标签,顺便说一句。即使目标是将您的查询用于 ajax 自动完成,您的问题也与此无关。

标签: jquery mysql ajax autocomplete jquery-autocomplete


【解决方案1】:

尝试在列名之间使用“+”

SELECT investigators.name + ' ' + ancientones.name + ' ' + monsters.name FROM investigators, ancientones, monsters WHERE ((investigators.name LIKE'a%') OR (ancientones.name LIKE'a%')OR (monsters.name LIKE'a%'))

【讨论】:

    【解决方案2】:

    您需要使用 UNION(或 UNION ALL 来保留重复项)

    SELECT name
    FROM investigators
    WHERE name like 'a%'
    UNION
    SELECT name 
    FROM anientones
    WHERE name like 'a%'
    UNION
    SELECT name
    FROM monsters
    WHERE name like 'a%'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2014-05-20
      • 2019-12-26
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多