A
A
Albert2020-09-04 18:52:16
MySQL
Albert, 2020-09-04 18:52:16

SQL query selection from 2 tables?

Hello good people!

I have a question of abstract content, I haven’t touched SQL for a long time, but they asked me a question, I answered it and now I smell what they thought of me.

For example, there are 2 tables:

1Table
id , name

2Table
worker_id, date, value You

need to select workers who did not receive a salary in the specified month / date.

Actually what I replied:

SELECT id name FROM table1 LEFT JOIN b as (SELECT worker_id date value FROM table2 WHERE data = 'интересующая дата' ) ON table1.id = b.worker_id WHERE date = 'null'

Well, since I wasn’t quite sure and the query was composed like this according to the echoes of memory, I added:
Logically, the query should return the id and name fields, those employees who, after attaching the virtual table b obtained as a result of the query, the date field is NULL .

Does my reasoning have a place in the SQL world?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2020-09-04
@6ETuK

First - always format the code, and it's easier for you, and there are fewer errors, and it's nice for people to look ...

SELECT id name // выполнится, но не так как задумано, не хватает запятых
FROM table1 
LEFT JOIN b as // здесь все совсем плохо
(  
           SELECT worker_id date value //тут уже даже не выполнится
           FROM table2 
           WHERE data = 'интересующая дата'  //date же!
) 
ON table1.id = b.worker_id //могло бы даже работать, но выше куча ошибок
WHERE date = 'null' // так работать не будет. date IS NULL будет, но у вас вроде нужно value проверять?

Among other things, there should have been just a join of 2 tables, without any dances, and the date, apparently, should be in the interval, and not a single number, although it’s not entirely clear what is there in general and how it is stored. Also, field names must be framed with `back quotes`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question