使用 ORDER BY 加入两个 sql 查询

Join two sql queries with ORDER BY

SELECT item_name, alt_names 
FROM antidepressants 
WHERE status = 'verified' 
ORDER BY priority, item_name

我在 PHP 中使用上述查询根据优先级对列进行排序,优先级设置为 1-6,其余记录设置为优先级 1000。

这是 table

的示例
item_name   alt_name    priority
a           null        1000
b           null        1000
c           null        1000
d           null        1000
e           null        1000
x           f,g         1
y           h           2
z           null        3

我希望的是以下内容

x
y
z
a
b
c
d
e
f
g
h

但是因为 alt_names 列共享 item_name 的优先级,它会像这样出现

x
f
g
y
h
z
a
b
c
d
e

看来我需要一种方法来执行两个查询,然后在一个查询中合并。我不想有两个单独的查询,然后使用 php 将它们组合起来。

编辑:我的 php 处理定界的代码

function populateAutocomplete()
{
    $query       = "SELECT item_name, alt_names FROM antidepressants WHERE status = 'verified' ORDER BY priority, item_name";
    $result_conn = conn($query);

    $autocomplete = array();
    if ($result_conn[0]->num_rows > 0) {
        while ($row = $result_conn[0]->fetch_assoc()) {
            $altnames = explode(",", $row['alt_names']);
            //var_dump($altnames);
            array_push($autocomplete, $row['item_name']);
            $autocomplete = array_merge($autocomplete, $altnames);
            }
        } 
    else {
        echo "0 results";
    }
    $result_conn[1]->close();
    foreach($autocomplete as &$val) {
        $val = trim($val);
    }
    $autocomplete = array_filter($autocomplete);
    $implode  = implode(",", $autocomplete);
    echo $implode;
}

因为您的示例显示 1000 排在 2 上方,我假设 priorityvarchar 列。

将其转换为 int 进行排序会得到更好的结果:

order by cast(priority as int), item_name

不幸的是,你问的问题比修复你的 table 定义更麻烦。每当您认为逗号分隔字段是个好主意时,请提醒自己事实并非如此,并且您应该规范化 tables.

您应该拥有第二个 table,而不是 table 中的备选名称列表,纯粹用于备选名称。例如:

create table alternative_names (
       item_name varchar(25),
       alt_name varchar(25)
);

有了这样的table,构造一个UNION查询来获得你想要的结果就很简单了。在这里,我们为所有替代名称分配默认优先级 1000:

select * from (
  SELECT item_name, priority
    from antidepressants
  UNION ALL
  SELECT alt_name, 1000
    from alternative_names
  ) q
order by priority asc, item_name asc;

有这个的演示 here

鉴于事先无法知道您的单个 table 中有多少个替代名称,因此没有一个简单的查询可以满足您的需求。

就像我在评论中所说的那样,我不是 Php 专家(我更懂 C# + SQL),但我知道逻辑,我认为你需要这样的东西:

function populateAutocomplete()
{
    $query       = "SELECT item_name, alt_names FROM antidepressants WHERE status = 'verified' ORDER BY priority, item_name";
    $result_conn = conn($query);

    $autocomplete = array();
    if ($result_conn[0]->num_rows > 0) {
        while ($row = $result_conn[0]->fetch_assoc()) { //While to add item_name to array
            array_push($autocomplete, $row['item_name']);
            }
            $row = 0;
        while ($row = $result_conn[0]->fetch_assoc()) { //while to add alt_names to end of array
            $altnames = explode(",", $row['alt_names']);
            //var_dump($altnames);
            $autocomplete = array_merge($autocomplete, $altnames);
        }

        } 

    else {
        echo "0 results";
    }
    $result_conn[1]->close();
    foreach($autocomplete as &$val) {
        $val = trim($val);
    }
    $autocomplete = array_filter($autocomplete);
    $implode  = implode(",", $autocomplete);
    echo $implode;