Join SQL Tables

0

I have two tables:

curso curso_configurar

With the following structure:

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

I tried to do this:

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, only the courses that have already been configured are ready, I need to do the reverse, list the courses that were NOT configured.

How can I mount?

    
asked by anonymous 24.03.2018 / 16:47

1 answer

3

Simply select the courses that are not registered 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 treatment of lists in programming languages, SQL also offers more efficient features for selecting data and applying constraints on it, and these features must be leveraged to avoid problems with performance in the future.

In the case of querying "select the courses that have already been configured" you should simplify it. Like this:

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 a miracle.

    
24.03.2018 / 17:04