10

Refer to the output table with specific fields

 2 years ago
source link: https://www.codesd.com/item/refer-to-the-output-table-with-specific-fields.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Refer to the output table with specific fields

advertisements

I have two tables:

Users. Where ID is the primary key

++++++++++++++++++
+ID......... Name+
+1 ......... Alex+
+2 ......... Rony+
+3 ......... Fred+
++++++++++++++++++

Order: Where Order_id is the primary key, and User_Id is a foreign key

Order_Id ............. User_ID ............ Day
123456   .............    3    ............ Monday
123457   .............    2    ............ Tuesday
123458   .............    1    ............ Friday

Now I would like to output the record having an order_id = 123458, however, I don't want the output to be :

123458   .............    1    ............ Friday

I want it to display the name Alex instead of value 1.

I want it like this:

Order_id .............    Name    ............ Day
123458   .............    Alex    ............ Friday

For example, if I do:

SELECT * FROM ORDERS WHERE ORDER_ID = 123458 it will display this output:

123458   .............    1    ............ Friday

As said before, I want the name Alex to be displayed instead of value 1.


As d_onimic say is better if you start with a sql tutorial. Here Is a sample of using INNER JOIN and creating as Alias for the table names

SELECT O.OrderID, U.Name, O.Day
FROM ORDERS as O
INNER JOIN Users as U
  ON O.UserID = U.ID
WHERE ORDER_ID = 123458


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK