简体   繁体   English

如何用不同的列但按一个特定列排序的UNION ALL SELECT MySQL查询?

[英]How to do a UNION ALL SELECT MySQL query with various columns but order by one specific column?

One problem is that they have different named columns 一个问题是它们具有不同的命名列

So for example 所以举个例子

Table 1
- id
- value_a
- value_b

Table 2
- sid
- value_c
- value_d

I need something like 我需要类似的东西

SELECT id,value_a,value_b AS results FROM table_1 UNION ALL SELECT sid,value_c,value_d AS results FROM table_2 ORDER BY value_a/value_c LIMIT 30

I realize the value_a/value_c isn't what I'd actually do but that's what I mean. 我意识到value_a / value_c并不是我真正要做的,但这就是我的意思。

Right now I am able to get the different columns and order alphabetically, I'm just trying to avoid querying again to first figure out which table the current row came from and then grabbing the rest, right now what I've got is 现在,我能够按字母顺序获取不同的列和顺序,我只是在尝试避免再次查询以首先弄清楚当前行来自哪个表,然后获取其余的行,现在我所拥有的是

SELECT value_a AS name FROM table_1 UNION ALL SELECT value_c AS name FROM table_2 ORDER BY name LIMIT 30

Can I do what I'm trying to do? 我可以做我想做的事吗? I've been going through a bunch of stack overflow threads, not really getting it to work. 我一直在经历一堆堆栈溢出线程,但并没有真正使它工作。

edit: this might be what I want 编辑:这可能是我想要的

Using union and order by clause in mysql 在mysql中使用union和order by子句

edit: 编辑:

The answer I came to from posts here and from other stackoverflow posts: 我来自这里的帖子和其他stackoverflow帖子的答案:

So you've got your two tables. 这样您就有了两个桌子。

To select from both and order by a specific column and also echo out each specific column: 要从两者中选择并按特定列排序,并回显每个特定列:

SELECT id AS id, value_a AS value_1, value_b AS value_2, 1 as tblname FROM Table_1 UNION ALL SELECT sid as id, value_c as value_1, value_d as value_2, 2 as tblname FROM Table_2 ORDER BY value_1 LIMIT 30

Then you can write a catch like this: 然后,您可以编写如下代码:

if ($row['tblname'] === "1") {
  // echo out / do stuff to this specific set of columns
  echo $row['value_1']; // echos out value_a
}
else if ($row['tblname'] === "2") {
  // echo out / do stuff to this specific set of columns
  echo $row['value_1']; // echos out value_c
}

Not saying this is the best route, but this single query that gets 30 rows is better than the same command but using more than one query becoming 31 queries (1 query every time you want to get the data knowing what you're looking for in what table) 并不是说这是最佳途径,但是此单查询可以获得30行比同一个命令要好,但是要使用多个查询就变成31个查询(每当您要使数据知道要查找的内容时就使用1个查询)什么桌子

Something like this? 像这样吗

SELECT
    t.id
    t.results
from
    (
        SELECT 
            id
            ,value_a
            ,value_b AS results 
        FROM table_1 
        UNION ALL 
        SELECT 
            sid
            ,value_c
            ,value_d AS results 
        FROM table_2 
    ) as t
ORDER BY results 
LIMIT 30

Not exactly sure about your requirements. 不确定您的要求。 But I assume you need to order the columns alphabetically after the union all and still be able to tell which table the row is actually from. 但是我认为您需要在所有并集之后按字母顺序对列进行排序,并且仍然能够确定该行实际来自哪个表。

Correct me if I am wrong. 如果我错了,请纠正我。

I would go with a query like this: 我将使用这样的查询:

SELECT value_a AS name, 1 as tblname FROM table_1 
UNION ALL 
SELECT value_c AS name, 2 as tblname FROM table_2 
ORDER BY name 

This will display the table no accordingly. 这将相应地显示表格编号。

To allow value_b from table_1 and value_d from table_2 to be displayed in separate columns: 要允许表_1中的value_b和表_2中的value_d显示在单独的列中:

SELECT value_a AS name, value_b, null as value_d, 1 as tblname FROM table_1 
UNION ALL 
SELECT value_c AS name, null as value_b, value_d, 2 as tblname FROM table_2 
ORDER BY name 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM