【问题标题】:How to use UNION in this case (MySQL) [duplicate]在这种情况下如何使用 UNION (MySQL) [重复]
【发布时间】:2016-11-15 17:48:44
【问题描述】:

我在数据库中有两个表(table_1table_2),每个表都有一个名为 Name 的相互列。

我目前正在使用以下代码仅从table_1 导入一些数据(Name,status):

/* database section start */
    $mysqli = new mysqli("z","z","z","z");

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
/* database section end */

// Choose Relevant items, and turn them into an array
$item_array = array(
'item1', 
'item2',
'item3'
);

//implode items, turn into string
$item_implode = join("','", $item_array);

//declare an overall array for result
$product = array();

$result = $mysqli->query("SELECT Name, Status as status from table_1 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');");

while($row = $result->fetch_assoc()) {

    $product_name = $row['Name'];
    // find all keys in $item_array which value is the products
        $keys = array_keys($item_array, $product_name);
    foreach ($keys as $key) {

        // add values for these keys
        $product[$key + 1]["Name"] = $row['Name'];
        $product[$key + 1]["status"] = $row['status'];//

    }
}

如果我还想从table_2 导入数据(例如,名为colordate_added 的列),我应该在此代码中添加什么?

我的目标是拥有这些钥匙:

$product[$x]["Name"]
$product[$x]["status"]
$product[$x]["color"]
$product[$x]["date_added"]

编辑:

那个长线程没有回答我的问题。该代码与我正在使用的代码不相似,我想知道如何在这个具体案例中准确地应用 UNION。

我尝试过使用:

$result = $mysqli->query

("(SELECT Name, status as status from table_1 ) 

UNION

(SELECT Name, color as color from table_2 )

where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');");


while($row = $result->fetch_assoc()) {

但我在最后一行遇到致命错误。

EDIT2:

还是报错:

$result = $mysqli->query

("(SELECT Name, status as statu table_1 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode') ) 
UNION (SELECT color from table_2  where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode') );");

while($row = $result->fetch_assoc()) {

【问题讨论】:

标签: php mysql database


【解决方案1】:
$result = $mysqli->query("SELECT table_1.Name, table_1.Status,table_2.color,table_2.date_added from table_1 inner join table_2 on table_1.Name=table_2.Name where table_1.Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');");

【讨论】:

    【解决方案2】:
    (SELECT Name, Status as status FROM table_1 t1)
    UNION
    (SELECT color, date_added FROM table_2 t2)
    

    【讨论】:

    • 请查看我编辑的问题,我不明白如何应用该 UNION。
    • 在 select 中使用 where/order 等子句:(SELECT Name, Status as status FROM table_1 t1 where ... order by ...) UNION (SELECT color, date_added FROM table_2 t2 where ... order by ...)
    • 仍然出现错误,请参阅我的第二次编辑
    【解决方案3】:

    更改您的查询,并尝试使用:INNER JOIN table_2

    我无法为您查询,因为我没有您的数据...

    【讨论】:

      【解决方案4】:

      使用连接构建您的查询。

      $query="SELECT t1.Name, t1.Status as status, t2.color, t2.date_added) from "
        ."table_1 t1 "
        ."LEFT JOIN table_2 t2 ON t2.Name "
        ."where t1.Name IN ('$item_implode') "
        ."AND t2.Name = t1.Name "
        ."ORDER BY FIELD (t1.Name, '$item_implode');";
      

      【讨论】:

      • 如果可能的话,我更喜欢一种不会改变我的代码整体结构的解决方案
      猜你喜欢
      • 2014-06-06
      • 1970-01-01
      • 2013-03-15
      • 2013-05-19
      • 2022-01-23
      • 2015-11-30
      • 2017-05-16
      • 2012-10-06
      • 2011-05-18
      相关资源
      最近更新 更多