从多个表中检索多个行

时间:2017-10-30 20:54:11

标签: php mysql database-design inner-join multiple-columns

我正在尝试从多个表中检索多行,但我认为我没有以正确的方式执行此操作。该项目是一个在线商店,我有3个表:订单,订单详情和服务,都与ID链接:

我在orderdetails表中有订单ID和服务ID,这意味着我为链接到服务ID的篮子上的每个项目插入一行以查看哪个服务,以及订单ID以检查哪个订单是。例如:

services table
-
service_id|name   |price
------------------------
2         |Tech   |100
------------------------
4         |Support|150
------------------------
10        |Mainten|50
------------------------

orders table
-
order_id|customer_id|name|lastname
----------------------------------
10      |16         |John|Smith
----------------------------------

orderdetails table
-
orderdetails_id|order_id|service_id|price|quantity
--------------------------------------------------
1              |10      |2         |100  |4
--------------------------------------------------
2              |10      |4         |150  |2
--------------------------------------------------
3              |10      |10        |50   |1
--------------------------------------------------

我在orderdetails表上插入服务价格,因为服务价格可能会在客户订购后发生变化。

此刻我有这个问题:

$query = $this->db->prepare(
'SELECT orders.*, orderdetails.*, services.*
FROM  orders
LEFT JOIN orderdetails
ON orderdetails.order_id = orders.order_id
LEFT JOIN services
ON orderdetails.service_id = services.service_id
WHERE orders.order_id = ?
AND orders.customer_id = ?');

我得到了这个结果:

    stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => Tech
    [lastname] => Smith
    [orderdetails_id] => 1
    [service_id] => 2
    [price] => 100
    [quantity] => 4
)
stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => Support
    [lastname] => Smith
    [orderdetails_id] => 2
    [service_id] => 4
    [price] => 150
    [quantity] => 2
)
stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => Mainten
    [lastname] => Smith
    [orderdetails_id] => 3
    [service_id] => 10
    [price] => 50
    [quantity] => 1
)

我有两个问题。第一个问题是我在订单表和服务表中有相同的列名。第二个是查询返回所有信息(因为我知道我不是很好查询),但我希望收到这样的信息:

stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => John
    [lastname] => Smith
    [orderdetails_id] => 1
    [service_id] => 10
    [price] => 50
    [quantity] => 1
    [service_name] => Mainten
    [orderdetails_id2] => 2
    [service_id2] => 4
    [price2] => 150
    [quantity2] => 2
    [service_name2] => Support
    [orderdetails_id3] => 3
    [service_id3] => 2
    [price3] => 100
    [quantity3] => 4
    [service_name3] => Tech
)

我的意思是,我不是SQL Queries的专家,而且我读了很多,但我认为你们可以帮我解决这个问题,因为我还有其他两个表可以链接:客户服务工作者将获得处理订单,以及将收到订单的区域表。

我使用此代码获取对象:

$array = array(); 
while($loop = $result->fetch_object()){ $array[] = $loop; }
return $array;

1 个答案:

答案 0 :(得分:0)

问题是您使用fetch_object()来获取结果,但是您没有在查询中重命名具有相同名称的列,并且因为您不能拥有两个不同的对象属性其余列的名称将被丢弃。

您可以使用其他方法获取fetch_row()等值,也可以更改查询以重命名具有相同名称的列,例如:

Route


请注意,如果SELECT orders.*, orderdetails_id, service_id, orderdetails.price as detail_price, quantity, services.name as service_name, services.price as service_price FROM orders LEFT JOIN orderdetails ON orderdetails.order_id = orders.order_id LEFT JOIN services ON orderdetails.service_id = services.service_id WHERE orders.order_id = ? order_id的主键,则您不需要在orders条件中使用customer_id,并且如果主键是两列(即,您可以为不同的客户提供相同的订单ID)我建议更改它并仅使用where