Double query in 1 query

Asked

Viewed 143 times

0

I have 2 tables one of users and another of subscribers, I have an Internet that sends to the table of subscribers the email of the user who registered and the name of the channel

1st users

id | usuario  | meucanal 
1  | [email protected]  | portados fundos 
2  | [email protected]  | parafernalha
3  | [email protected]  | Ei nerd
4  | [email protected]  | Canal Nostalgia

2nd entries

id | usuario | se_inscreveu_no_canal
1  | [email protected]   | Ei nerd
2  | [email protected]   | parafernalha

Summarizing what I need is to make a SELECT list only the channels that the user has not yet registered in the case of the example the [email protected] needs to be displayed only the Nostalgia Channel

$sql = "SELECT * FROM usuarios WHERE canal NOT EXIST IN(SELECT * FROM inscricoes WHERE o_usuario NOT IN se_inscreveu_no_canal) ORDER BY RAND();

I’m not a professional in the area, I just program as a hobby to make things simpler and this type of consultation is more advanced if you can help me in a way to solution since I thank you.

Follows a bit of structure for better understanding(well summarized)

php.

enter your email and channel name

INSERT INTO usuarios (usuario, canal) VALUES ('$_POST[email]',$_POST[canal]')"; READY REGISTER

listarcanais.php ( the user in this part is already logged in $user=Session[email]) SELECT * FROM usuarios ORDER BY canal LIMIT 1"; echo " CLICK HERE FOR YOURSELF ";

subscribe.php ( the user in this part is already logged in $user=Session[email]) $canal= $GET[canal]; INSERT INTO entries (user, channel) VALUES ('$user,$channel')"; //redirect to listarcanais.php PRONTO JA APPEARS IN THE COMIC BOOK THAT THE USER HAS ALREADY ENTERED IN THAT CHANNEL

listarcanais.php <<< as in my head think the system should read

SELECT CHANNEL FROM (table users) WHERE the (user=$user) DID NOT register ( in the table entries)

roughly: 1° a select that takes the (email/$user) and compares the USERS and SUBSCRIPTIONS tables in order to delete the other email that is not the $user one

done this 2° a select that lists all channels of the USUARIOS table and compares with all channels of the INSCRIPTIONS table. Those who are not equal will be listed

  • 1

    Your database structure can (I believe it should) be improved, I suggest a user table and another channel, in a relation N:N where the generated third table contains the user id and the id of the channels it is inscribed

2 answers

-1


SELECT
    distinct(canal)
FROM
    usuarios
WHERE
    email <> '$email'
    AND
    canal NOT IN (
        SELECT canal FROM inscricoes WHERE email = '$email'

   )

-2

There are only these two tables, there is no channel table? How is all existing channels recorded?

Having three tables: Usuarios, Canais and Inscricoes, where Inscricoes is many-to-many relationship between Usuarios and Canais, so just bring the Canais that do not exist in Inscricoes for Usuarios.

select u.nome as usuario, c.nome as canal
from usuarios u, canais c
where not exists (select 1 from inscricoes i where i.cod_canal =c.cod_canal and i.cod_usuario = u.cod_usuario);
  • then all registered channels are in the user table each user can only have 1 channel, at the time the user will register in the system it already gives the name of the channel basically only caught the email and the name of the channel

  • I suggest you do as suggested by William, make a list of many for many, so it will be normalized, simpler for maintenance and holding consultations

Browser other questions tagged

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