INNER JOIN in MySQL - mysql

There are my two tables:
Table name: FRIENDS
+-------+---------+---------+-------------------+
| id | firstName | lastName | city |
+-------+--------------+------------+-----------+
| 1 | dudi | edri | london |
| 2 | maor | azulay | madrid |
| 3 | batel | azulay | tel aviv |
| 4 | nir | cohen | barcelona |
| 5 | evia | perez | miami |
| 6 | neria | perez | new-york |
| 7 | nevo | kakoun | roma |
+-------+---------+---------+-------------------+
Table name: ORDERS
+-------+---------+---------+-----------------+
| id | firstName | amount | status |
+-------+--------------+----------+-----------+
| 1 | dudi | 5684 | shipped |
| 2 | maor | 4896 | shipped |
| 3 | batel | 2496 | delay |
+-------+--------------+----------+-----------+
my question is:
I want the friends that have no order.
The answer:
| 4 | nir | cohen | barcelona |
| 5 | evia | perez | miami |
| 6 | neria | perez | new-york |
| 7 | nevo | kakoun | roma |
+-------+---------+---------+-------------------+
how i wrote the query with inner join.
Thanks.

You shouldn't use firstName in ORDERS as Foreign Key. The foreign key should reference the primary key. Use ID from FRIENDS like:
Table name: ORDERS
+-------+---------+---------+-----------------+
| id | friendID | amount | status |
+-------+--------------+----------+-----------+
| 1 | 1 | 5684 | shipped |
| 2 | 2 | 4896 | shipped |
| 3 | 3 | 2496 | delay |
+-------+--------------+----------+-----------+
And query using LEFT OUTER JOIN:
SELECT f.*
FROM FRIENDS f
LEFT JOIN ORDERS o
ON f.ID = o.friendID
WHERE o.ID IS NULL;
LiveDemo
Another possibility is to use correlated subquery:
SELECT f.*
FROM FRIENDS f
WHERE NOT EXISTS (SELECT 1
FROM orders o
WHERE o.friendID = f.ID);
LiveDemo2
Tables:
CREATE TABLE friends(
id INTEGER NOT NULL PRIMARY KEY -- you can add AUTO_INCREMENT if needed
...
);
CREATE TABLE orders(
id INTEGER NOT NULL PRIMARY KEY
,friendID INTEGER NOT NULL
,FOREIGN KEY (friendID) REFERENCES friends(id)
...
);

Related

Mysql select with multiple tables and multiple foreign keys

If I have three tables like:
suppliers (sID: integer, sName: varchar, description: varchar); With sID as primary key
+-----+-----------+-------------+
| sID | SName | description |
+-----+-----------+-------------+
| 1 | Supplier1 | Desc1 |
| 2 | Supplier2 | Desc2 |
| 3 | Supplier3 | Desc3 |
| 4 | Supplier4 | Desc4 |
| ... | ... | ... |
+-----+-----------+-------------+
products (pID: integer, pName: varchar, size: varchar); With pID as primary key
+-----+----------+------+
| pID | pName | size |
+-----+----------+------+
| 1 | Product1 | S |
| 2 | Product2 | M |
| 3 | Product3 | B |
| 4 | Product4 | M |
| ... | ... | ... |
+-----+----------+------+
menu (sID: integer, pID: integer, cost: varchar); with sID and pID as foreign keys
+-----+-----+------+
| sID | pId | cost |
+-----+-----+------+
| 1 | 4 | 10 |
| 2 | 16 | 20 |
| 8 | 1 | 5 |
| 8 | 2 | 8 |
| ... | ... | ... |
+-----+-----+------+
The only relation between suppliers and products is given by the menu table.
How can I select the names of all suppliers that supply all products?
Thanks for any help!
Here is how you would get the ids. Simply aggregate and count:
select m.sid
from menu m
group by m.sid
having count(distinct pid) = (select count(*) from products);
I'll leave it to you to get the supplier name, which you can do using in, exists, join, or even a correlated subquery.

mysql query for multiple foreign keys

mysql tables are as follows
+------------+----------------+----------------+
| booking_id | boarding_point | dropping_point |
+------------+----------------+----------------+
| 1 | 2 | 4 |
| 2 | 1 | 2 |
+------------+----------------+----------------+
+-------------+---------------+
| location_id | location_name |
+-------------+---------------+
| 1 | chennai |
| 2 | coimbatore |
| 3 | tiruppur |
| 4 | erode |
| 5 | salem |
+-------------+---------------+
boarding_point and dropping_point are foreign keys for location_id.
Now I want the select query to display like
+------------+----------------+----------------+
| booking_id | boarding_point | dropping_point |
+------------+----------------+----------------+
| 1 | coimbatore | erode |
| 2 | chennai | coimbatore |
+------------+----------------+----------------+
can anyone please suggest me the query to display like above.
Join the booking table twice to the location table:
SELECT
b.booking_id,
t1.location_name,
t2.location_name
FROM booking b
INNER JOIN location t1
ON b.boarding_point = t1.location_id
INNER JOIN location t2
ON b.dropping_point = t2.location_id;
Demo

Count from two different tables with same column name and group tehm if same name

I have two tables with the same structure:
Table -1:
+----------------------+--------------+-----+
| Field | Type | Key |
+----------------------+--------------+-----+
| id | int(5) | PRI |
| country | varchar(500) | |
+----------------------+--------------+-----|
Table -2:
+----------------------+--------------+-----+
| Field | Type | Key |
+----------------------+--------------+-----+
| id | int(5) | PRI |
| country | varchar(500) | |
+----------------------+--------------+-----|
The data in the table will be as following:
Table -1:
+----+---------+
|id | country |
+----+---------+
| 1 | A |
| 2 | B |
| 3 | A |
| 4 | A |
| 5 | B |
+----+---------+
Table -2:
+----+---------+
|id | country |
+----+---------+
| 1 | A |
| 2 | B |
| 3 | B |
| 4 | B |
| 5 | B |
+----+---------+
When i use the following query statement :
SELECT country, COUNT(*) AS result FROM table-1 GROUP BY country UNION SELECT country, COUNT(*) AS result FROM table-2 GROUP BY country;
I get the result as following:
+--------+---------+
|country | result |
+--------+---------+
| A | 3 |
| B | 2 |
| A | 1 |
| B | 4 |
+--------+---------+
Which shows the count of table-1 and table-2 seperately. But i want the count to be combined as following:
+--------+---------+
|country | result |
+--------+---------+
| A | 4 |
| B | 6 |
+--------+---------+
Thanks in advance.
select t.country,count(t.country)
from
(
select * from table1
union all
select * from table2
) t group by t.country
Demo

select data from multiple tables by matching multiple columns

I have 3 tables like so:
Table User
+---------+-----------------+
| UserId | UserName |
+---------+-----------------+
| 1 | one#test.com |
| 2 | two#test.com |
| 3 | three#test.com |
| 4 | four#test.com |
+---------+-----------------+
Table Employee
+-------------+----------------+------------+----------+---------+
| EmployeeId | Email | Department | Position | Duty |
+-------------+----------------+------------+----------+---------+
| 1 | one#test.com | Accounting | Manager | Aproval |
| 2 | two#test.com | Accounting | Manager | NULL |
| 3 | three#test.com | Marketing | Staff | NULL |
| 4 | four#test.com | Purchasing | Staff | NULL |
+-------------+----------------+------------+----------+---------+
Table Authorization
+------------------+----------------+------------+----------+----------+
| AuhtorizationId | Level | Department | Position | Duty |
+------------------+----------------+------------+----------+----------+
| 1 | 1 | Accounting | Manager | NULL |
| 2 | 2 | Marketing | Staff | NULL |
| 3 | 3 | Purchasing | Staff | NULL |
| 4 | 4 | Accounting | Manager | Approval |
+------------------+----------------+------------+----------+----------+
How to construct a MySQL query to retrieve UserId, UserName/Email, Level, Department, and Position?
If I understand correctrly all the table relations
SELECT UserID, UserName, Level, Department, Position FROM User
INNER JOIN Employee ON UserId=EmployeeID INNER JOIN Authorization ON UserId = AuhtorizationId
Query:
SQLFiddleExample
SELECT
u.`UserID`,
u.`UserName`,
a.`Level`,
e.`Department`,
e.`Position`
FROM `User` u
LEFT JOIN `Employee` e
ON u.`UserId` = EmployeeID
LEFT JOIN `Authorization` a
ON u.`UserId` = a.`AuhtorizationId`
Result:
| USERID | USERNAME | LEVEL | DEPARTMENT | POSITION |
-----------------------------------------------------------
| 1 | one#test.com | 1 | Accounting | Manager |
| 2 | two#test.com | 2 | Accounting | Manager |
| 3 | three#test.com | 3 | Marketing | Staff |
| 4 | four#test.com | 4 | Purchasing | Staff |
SELECT User.UserID, User.UserName, Authorization.Level, Authorization.Department, Authorization.Position FROM User INNER JOIN Employee ON User.UserId=Employee.EmployeeID INNER JOIN Authorization ON Employee.Department = Authorization.Department WHERE Employee.Position=Authorization.Position AND Employee.Duty=Authorization.Duty

LEFT JOINs with multiple tables

I have following tables:
table users - PRIMARY KEY (user_id)
+---------+----------+-----------+
| user_id | username | realname |
+---------+----------+-----------+
| 1 | peterpan | Peter Pan |
| 2 | bobfred | Bod Fred |
| 3 | sallybe | Sally Be |
| 6 | petersep | Peter Sep |
+---------+----------+-----------+
table users_groups - PRIMARY KEY (user_id, group_id)
+---------+----------+
| user_id | group_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 6 |
| 3 | 9 |
| 6 | 6 |
| 6 | 9 |
+---------+----------+
table game - PRIMARY KEY (id)
+----+-------+
| id | game |
+----+-------+
| 1 | Game1 |
| 2 | Game2 |
| 6 | Game6 |
| 9 | Game9 |
+----+-------+
table groups - PRIMARY KEY(group_id)
+----------+--------------+---------------+
| group_id | group_name | group_desc |
+----------+--------------+---------------+
| 1 | Groupname1 | Description1 |
| 2 | Groupname2 | Description2 |
+----------+--------------+---------------+
table group_game - PRIMARY KEY(group_id, game_id)
+----------+----------+
| group_id | game_id |
+----------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 6 |
| 2 | 9 |
+----------+----------+
I want to display this (like a group list):
+----+------------+--------------+---------------------+--------------+
| id | group name | group desc | group members | group games |
+----+------------+--------------+---------------------+--------------+
| 1 | GroupName1 | Description1 | Peter Pan, Bob Fred | Game1, Game2 |
| 2 | GroupName2 | Description2 | Sally Be, Peter Sep | Game6, Game9 |
+----+------------+--------------+---------------------+--------------+
Now I have this query but it gives me no rows (no error, just zero rows):
SELECT
g.group_name,
g.group_id,
g.group_desc,
GROUP_CONCAT(DISTINCT ga.game SEPARATOR ', ') AS games,
GROUP_CONCAT(DISTINCT u.realname SEPARATOR ', ') AS users
FROM groups g
LEFT JOIN users_groups ug1
ON g.group_id=ug1.group_id
LEFT JOIN users u
ON ug1.user_id=u.user_id
LEFT JOIN group_game gg
ON g.group_id=gg.group_id
LEFT JOIN game ga
ON gg.game_id=ga.id
GROUP BY g.group_name
How can I solve this problem or how can I write this query?
I just want to show a group list with all information (like group information, users of the groups, games of this group).