Jump to content

Selecting identically named rows in two tables


BuildMyWeb

Recommended Posts

the text in red is where im confused. how do i assign values for $orders_id and $shipping_id from their respective tables in one query? i imagine it can be done. i dont have to do 2 separate queries, do i?

 

$query_order = "SELECT orders.id, shipping.id FROM orders, shipping WHERE orders.order_num = shipping.order_num AND orders.order_num = \"$order_req\"";
$result_order = $db->query( $query_order );
$num_rows_order = $result_order->num_rows;

for( $i=0; $i < $num_rows_order; $i++ )
{
   $row_order = $result_order->fetch_assoc(); // put field values for the row in an array

   $orders_id = $row_order['id'];
   $shipping_id = $row_order['id'];
}

thanks Barand. so like this?

 

$query_order = "SELECT orders.id AS oid, shipping.id AS sid FROM orders, shipping WHERE orders.order_num = shipping.order_num AND orders.order_num = \"$order_req\"";
$result_order = $db->query( $query_order );
$num_rows_order = $result_order->num_rows;

for( $i=0; $i < $num_rows_order; $i++ )
{
    $row_order = $result_order->fetch_assoc(); // put field values for the row in an array

    $orders_id = $row_order['oid'];
    $shipping_id = $row_order['sid'];
}

Yes, and no

 

Aliases are right. If you are only expecting a single row then you don't want a for loop.

 

If you expect several rows the ids will be overwritten in each loop and you will only get the last ones

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.