Join SQL Tables

Asked

Viewed 36 times

0

I have two tables:

curso curso_configurar

With the following structure:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

My question is: I need to include within the curso_configurar, the courses that will be configured, until there is OK. However, when I add a new configuration, I cannot list the course that has already been configured.

I tried to do it this way:

SELECT * FROM (`curso`) LEFT JOIN `curso_configurar` ON `curso_configurar`.`cur_id`=`curso`.`cur_id` WHERE `curso_configurar`.`cur_id` NOT IN ('curso.cur_id') ORDER BY `cur_titulo` ASC

In this case, I list only the courses that have already been configured, I need to do the reverse, list the courses that have NOT BEEN configured.

How can I ride?

1 answer

3


Just select the courses that have no record in the settings table. So:

SELECT * 
FROM `curso` c
WHERE NOT EXISTS(SELECT 1 
                 FROM `curso_configurar` cc
                 WHERE cc.cur_id = c.cur_id )

Post Script:

As in the handling of lists in programming languages, SQL also offers more efficient resources to select the data and apply restrictions on them and these resources should be used to avoid performance problems in the future.

In the case of consultation for "select courses that have already been configured" you should simplify it. That way:

SELECT * 
FROM curso c 
    JOIN curso_configurar cc ON cc.cur_id = c.cur_id 
ORDER BY c.cur_titulo ASC

The query you wrote for this purpose, if it is working is only by miracle.

  • I did it this way: https://pastebin.com/PdQV8Wj8

  • php It seems Greek to me: I don’t know when you’re running, concatenating, declaring, getting the value or anything else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.