mysqli连接相同的列名

时间:2019-07-11 06:08:34

标签: mysql join

现在我有以下代码:

SELECT * FROM orders, products WHERE product_id=products.id AND `geleverd` = 0

这样返回: enter image description here

我还有一个按钮的代码:

echo "<a onclick='return window.confirm(\"Weet je zeker dat  ".$row['merk']." ".$row['model']." geleverd is?\")' href='change_order_status.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-ok' style='color:black'></span></a>";

但是由于某种原因,id返回9而不是1

这是$ row打印的输出

Array
(
    [0] => 1
    [id] => 9
    [1] => 2019-1
    [ordernummer] => 2019-1
    [2] => 9
    [product_id] => 9
    [3] => 2
    [aantal] => 2
    [4] => aaa
    [omschrijving] => aaa
    [5] => 0
    [geleverd] => 0
    [6] => 9
    [7] => Samsung
    [merk] => Samsung
    [8] => Tablet S2
    [model] => Tablet S2
    [9] => 1
    [threshold] => 1
)

和:

(
    [0] => 3
    [id] => 9
    [1] => 789
    [ordernummer] => 789
    [2] => 9
    [product_id] => 9
    [3] => 10
    [aantal] => 10
    [4] => test
    [omschrijving] => test
    [5] => 0
    [geleverd] => 0
    [6] => 9
    [7] => Samsung
    [merk] => Samsung
    [8] => Tablet S2
    [model] => Tablet S2
    [9] => 1
    [threshold] => 1
)

如何确定第一行的ID为1而不是9,第二行的ID为3而不是9

1 个答案:

答案 0 :(得分:0)

您需要为表使用Alias,并合并/重命名所需的列。

SELECT o.id as orderId, p.id as productId, -all the columns you need with the alias first- FROM orders as o, products as p WHERE o.product_id=p.id AND `geleverd` = 0