Ich möchte PHP $where
als Teil der großen Abfrage machen. Ich brauche so etwas wie:SELECT * FROM Tabelle WHERE `id` in ... und wo sum() weniger
SELECT *
FROM rf2aq_eb_events
WHERE id
IN (SELECT event_id
, SUM(number_registrants) summ
FROM rf2aq_eb_registrants
WHERE summ < event_capacity
);
Die rf2aq_eb_events
Tabelle wie folgt aussieht:
ID | event_capacity
1 | 7
2 | 5
3 | 9
Die rf2aq_eb_registrants
Tabelle:
ID | events_id | number_registrants
1 | 1 | 6
2 | 2 | 2
3 | 3 | 4
4 | 1 | 1
5 | 2 | 0
6 | 3 | 5
Ich brauche ausgewählte Veranstaltungen aus dem 'rf2aq_eb_events' für Veranstaltungen mit Menge Registrant < dann event_capacity. Es gibt Ereignis-ID = 2, um die Bedingung zu beantworten.
Ich habe $where[] = 'a.id IN (SELECT event_id FROM #__eb_registrants GROUP BY event_id HAVING sum(number_registrants) < a.event_capacity)';
versucht
Es ist wie SQL zu arbeiten, aber nicht in php in der gesamten Abfrage.
Unten habe ich PHP-Ergebnis gesetzt.
SELECT a.id, a.title, a.location_id,
a.event_capacity, a.event_date, a.individual_price,
a.thumb, a.early_bird_discount_date, a.early_bird_discount_amount,
c.name AS location_name
FROM #__eb_events AS a
LEFT JOIN #__eb_locations AS c
ON a.location_id = c.id
WHERE a.published =1
AND DATE(event_date) between date(CURDATE() + INTERVAL 7 DAY)
and date(CURDATE() + INTERVAL 26 DAY)
AND (
cut_off_date = "0000-00-00 00:00:00"
OR DATE(cut_off_date) between NOW() and date(CURDATE() + INTERVAL 26 DAY)
) AND a.id IN (
SELECT event_id
FROM #__eb_registrants
GROUP BY event_id
HAVING sum(number_registrants) < a.event_capacity
) AND a.id IN (
SELECT event_id FROM #__eb_event_categories
WHERE category_id IN (6,7)
) AND a.access IN (1,1)
ORDER BY a.event_date
LIMIT 4
Die Beispielabfrage wird nie funktionieren, aber es wäre gut, das erwartete Ergebnis in der Frage zu haben. –
Ich habe einen Fehler gefunden. Einfach a.event_capacity aus select entfernt. –