[英]I can`t make work my query having 2 fields set as primary key in a relational model in mysql + php
我正在嘗試為用戶事件編碼寄存器。
我在PHP編碼。
它必須顯示用戶注冊的所有事件和用戶未注冊的所有事件。
這是表中的主鍵:
USUARIO USUARIO_EVENTO EVENTO idlogin_user idlogin_user cod_event cod_event
它從名為$ userlogueado的cookie變量中獲取idlogin_user的ID。
實際發生的事情是假設我只有一個用戶,例如idlogin_user總是相同的,即使登錄的idlogin_user並不總是相同。
我已經嘗試使用UNION和JOIN以及其他句子,但是它們都不起作用。
我使用這兩個查詢:
$query = mysql_query (
"select distinct
evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE (evento.cod_event not in (select distinct usuario_evento.cod_event from usuario_evento))");
$query2 = mysql_query ("select evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
INNER JOIN usuario_evento
WHERE (evento.cod_event = usuario_evento.cod_event)
UNION
(select * from usuario_evento where idlogin_user = $userlogueado)
");
我要做的是為用戶注冊的事件和未注冊的事件創建兩個單獨的查詢。
假設user_events是多對多映射表,並且用戶只能注冊一次事件
SELECT * FROM user_events where user_id = '123';
然后選擇未注冊用戶的事件,並按用戶ID進行分組,因為將為該事件注冊許多用戶。
SELECT * FROM user_events where event_id NOT IN(
SELECT event_id FROM user_events where user_id = '123'
) GROUP BY event_id
嘗試這個
SELECT cod_event, idlogin_user, 1 AS participance
FROM EVENTO
JOIN USUARIO_EVENTO ON EVENTO.cod_event = USUARIO_EVENTO.cod_event
JOIN USUARIO ON USUARIO_EVENTO.idlogin_user = USUARIO.idlogin_user
UNION
SELECT outerEvento.cod_event, outerUsuario.idlogin_user, 0 AS participance
FROM EVENTO outerEvento
CROSS JOIN USUARIO outerUsuario
WHERE NOT EXISTS
(
SELECT cod_event, idlogin_user
FROM EVENTO innerEvento
JOIN USUARIO_EVENTO innerUsuarioEvento ON innerEvento.cod_event = innerUsuarioEvento.cod_event
JOIN USUARIO innerUsuario ON innerUsuarioEvento.idlogin_user = innerUsuario.idlogin_user
WHERE outerEvento.cod_event = innerEvento.cod_event AND outerUsuario.idlogin_user = innerUsuario.idlogin_user
)
列參與度顯示用戶是否參加(1)(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.