Answer the question
In order to leave comments, you need to log in
How to remake the request to get the desired response?
There are two tables, city and country.
DESCRIBE city;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| cid | int(11) | NO | | NULL | |
| name | varchar(255) | NO | | NULL | |
| salary | float | NO | | NULL | |
+--------+--------------+------+-----+---------+----------------+
4 rows in set (0.000 sec)
DESCRIBE country;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| is_here | tinyint(4) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.000 sec)
SELECT
city.id,
city.name,
country.name,
salary,
country.is_here
FROM city
LEFT JOIN country ON city.cid = country.id
GROUP BY city.id
+----+-----------+-------+--------+---------+
| id | name | name | salary | is_here |
+----+-----------+-------+--------+---------+
| 1 | Madrid | Spain | 4720 | 0 |
| 2 | Barcelona | Spain | 3000 | 0 |
| 3 | Rome | Italy | 5000 | 1 |
+----+-----------+-------+--------+---------+
3 rows in set (0.000 sec)
+----+-----------+-------+--------+---------+
| id | name | name | salary | is_here |
+----+-----------+-------+--------+---------+
| 1 | Madrid | Spain | 4720 | 0 |
| 2 | Barcelona | Spain | 3000 | 0 |
| 1 | Madrid | Spain | 6720 | 1 |
| 2 | Barcelona | Spain | 5000 | 1 |
| 3 | Rome | Italy | 5000 | 1 |
+----+-----------+-------+--------+---------+
5 rows in set (0.000 sec)
Answer the question
In order to leave comments, you need to log in
Something like this.
SELECT
city.id,
city.name,
country.name,
salary,
country.is_here
FROM city
LEFT JOIN country ON city.cid = country.id
union all
SELECT
city.id,
city.name,
country.name,
salary + 2000,
1
FROM city
LEFT JOIN country ON city.cid = country.id
where (country.id is null or country.is_here = 0)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question