Given the following tables
orders
+----+---------+---------+
| id | user_id | details |
+----+---------+---------+
| 1 | 2 | blue |
+----+---------+---------+
| 2 | 1 | red |
+----+---------+---------+
| 3 | 2 | yellow |
+----+---------+---------+
| 4 | 2 | cyan |
+----+---------+---------+
users
+---------+---------+---------+
| user_id | ph | name |
+---------+---------+---------+
| 1 | 123 | fred |
+---------+---------+---------+
| 2 | 456 | Stan |
+---------+---------+---------+
| 3 | 189 | Levi |
+---------+---------+---------+
I know how to select only one occurance of each user in the first table using distinct
SELECT DISTINCT user_id FROM orders
How could I pull just the phone number from users?
I could probably go for a loop and pick out each number like…
SELECT ph from users WHERE user_id = user_id
Can’t help thinking there might be a one liner query I could use.
result would be
123
456
Selecting Distinctively from the Orders table might be a heavy operation. Depending on how massive that table is going to become.
Maybe this one will be faster: