P
P
psylostlife2012-11-22 14:19:06
MySQL
psylostlife, 2012-11-22 14:19:06

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

5 answer(s)
V
vovich, 2012-11-22
@psylostlife

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

U
Urvin, 2012-11-22
@Urvin

UNION?

M
Melkij, 2012-11-22
@melkij

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'

?
This is how much I understand your structure of tables and relationships.

P
psylostlife, 2012-11-22
@psylostlife

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

A
anitspam, 2012-11-23
@anitspam

I want to note that sometimes two queries with one join can work faster than one with two joins.
suddenly faced with the growth of the project.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question