Answer the question
In order to leave comments, you need to log in
How to combine these two queries into one?
Hello. I’ve been trying this way and that way for an hour now, Google has been tormented by mana and examples, but I’m still stupid ...
user id - 1.
SELECT calendar.* FROM calendar /* Event table */
INNER JOIN subscribes_fields /* Table containing site subscription records */
ON calendar.id_object = subscribes_fields.id_field /* id is written to id_object, in this case we are looking for site id */
WHERE subscribes_fields.id_follower = 1 AND calendar.type = 'field' /* Accordingly, subscriber id - our event type is set for the site, since IDs can also be from other tables and types */
SELECT calendar.* FROM calendar /* Event table */
INNER JOIN subscribes_events /* Table containing event subscription records */
ON calendar.id = subscribes_events.id_event /* It's easier here, event subscriptions immediately contain the direct event id */
WHERE subscribes_events.id_follower = 1 AND subscribes_events.type = 'user' /* Where the subscriber is our user, the subscription type is users * /
Please help me combine these two queries into one?
Answer the question
In order to leave comments, you need to log in
select calendar.*
left join subscribers_fields
ON calendar.id_object = subscribes_fields.id_field AND subscribes_fields.id_follower = 1 AND calendar.type = 'field'
left join JOIN subscribes_events
ON calendar.id = subscribes_events.id_event and subscribes_events.id_follower = 1 AND subscribes_events.type = 'user'
where subscribers_fields.Id is not null OR subscribes_fields.id_field is not null
SELECT calendar.* FROM calendar /* Таблица событий */
INNER JOIN subscribes_fields /* Таблица, где содержатся записи подписок на площадки */
ON calendar.id_object = subscribes_fields.id_field /* В id_object записываются id, в данном случае мы ищем id площадок */
join JOIN subscribes_events ON calendar.id = subscribes_events.id_event and subscribes_fields.id_follower=subscribes_events.id_follower
WHERE subscribes_fields.id_follower = 1 AND calendar.type = 'field'
Actually, shortening:
SELECT calendar.*
FROM calendar AS c
LEFT JOIN subscribes_fields AS f
ON c.id_object = f.id_field
AND f.id_follower = 1
AND c.type = 'field'
LEFT JOIN subscribes_user_teams AS t
ON c.id_object = t.id_team
AND t.id_follower = 1
AND c.type = 'team'
LEFT JOIN subscribes_events AS e
ON c.id = e.id_event
AND e.id_follower = 1
AND e.type = 'user'
WHERE ((f.id IS NOT NULL OR f.id_field IS NOT NULL)
OR (t.id IS NOT NULL OR t.id_team IS NOT NULL))
AND c.d_start = 17
AND c.m_start = 11
AND c.y_start = 2012
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question