I need to select and update a table looking up values from other tables. The idea is to select some details from order, but get the names of the foreign key values using the id
For the select I tried:
SELECT [Order].order_date,
[Order].total_price,
[Order].order_details,
vehicle.reg_no,
Staff.name,
stock.name
FROM
Order,
vehicle,
staff,
stock
WHERE
order.id = @order_id
AND vehicle.id = Order.vehicle_id
AND staff.id = Order.staff_id
AND stock.id = Order.product_id
for the update that i tried
UPDATE order
SET
total_price = @total_price,
order_detail = @order_detail,
product_id = (select is from stock where name = @product_name),
customer_id = (select id from customer where name = @customer_name),
vehicle_regno = (select reg_no from vehicle where name = @name)
WHERE (id = @id)
both does not return anything. i hope am clear enough to get some help, but if not pls i will provide more info.
thanks for helping
You might want to try converting you INNER JOINs to a LEFT JOIN for the SELECT statement.
The reasons why this would make a difference is if either a) you allow nulls in the fk fields or b) you don’t have fk contraints which may point to a problem with your design.
Your update statement should update some rows provided that the value of @id exists in the ORDER table but as @Danny Varod already commented you won’t get rows back only the number of rows affected