B
B
basida42072021-10-21 14:35:03
MySQL
basida4207, 2021-10-21 14:35:03

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)


where cid - country id, name - name, salary - average salary for employees

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)


where name is the name, is_here is the region of presence

. At the moment I am using the following query:
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


What do I get
+----+-----------+-------+--------+---------+
| 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)


What would you like? Where there is no region of presence, "imitate" it and add +2000 USD to the salary
+----+-----------+-------+--------+---------+
| 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

1 answer(s)
A
alexalexes, 2021-10-21
@basida4207

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 question

Ask a Question

731 491 924 answers to any question