php


SELECT * FROM table WHERE `id` in … and where sum() less


I'd like to make PHP $where as part of big query. I need something like:
SELECT *
FROM rf2aq_eb_events
WHERE id
IN ( SELECT event_id
, SUM(number_registrants) summ
FROM rf2aq_eb_registrants
WHERE summ < event_capacity
);
The rf2aq_eb_events table looks like:
ID | event_capacity
1 | 7
2 | 5
3 | 9
The rf2aq_eb_registrants table:
ID | events_id | number_registrants
1 | 1 | 6
2 | 2 | 2
3 | 3 | 4
4 | 1 | 1
5 | 2 | 0
6 | 3 | 5
I need select events from the 'rf2aq_eb_events' for events with quantity of registrant < then event_capacity. There is event id = 2 respond the condition.
i've tried $where[] = 'a.id IN ( SELECT event_id FROM #__eb_registrants GROUP BY event_id HAVING sum(number_registrants) < a.event_capacity)';
It's working like SQL, but do not in php in the whole Query.
Below i've put php result.
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
You can't use the result of an aggregate (e.g. SUM) in a WHERE you can use it in a HAVING but you need to do a GROUP BY in this case as well:
SELECT * FROM `rf2aq_eb_events` e
WHERE `id` IN (
SELECT `event_id`
FROM `rf2aq_eb_registrants` r
GROUP BY `event_id`
HAVING sum(`number_registrants`) < `event_capacity`
)
You don't have to use subqueries.
SELECT `e`.* FROM `rf2aq_eb_events` as `e`
LEFT JOIN `rf2aq_eb_registrants` as `r`
ON `r`.`events_id`=`e`.`ID`
GROUP BY `r`.`events_id`
HAVING SUM(`r`.`number_registrants `) < `e`.`event_capacity`

Related Links

Linux Ubuntu PHP Version is different in CLI and in PHPINFO()
PHP curly braces inside strings colour in Notepad++
How to clean background captcha image using imagick
How can I get Magento Each Session values by using Magento frontend cookie value through Web Service?
WooCommerce displaying variable description after variable price
if else condition in jquery ajax response
php fastcgi is crashing if the url contains &start_debug=1
Having issue modifying theme code to replace logo URL
Combination of php_opcache and IIS causing intermittent 500 errors
DOMPDF class not found
wordpress APi integration
How Remove extra tag in xml in php
How to add a custom year drop-down in place of Export CSV drop-down on admin panel for custom grid and filter the collection?
Sending boolean values with $.ajax to PHP
Loop row in bootstrap every 3 columns
Get friends name/id list form graph api into array

Categories

HOME
azure-functions
seedstack
couchdb
enums
code-formatting
ant
malware
flurry
azure-data-lake
supercollider
mithril.js
save
pyresttest
reportviewer
stack-overflow
fasm
nsstring
game-maker
jquery-file-upload
amazon-elb
jodatime
fresco
robolectric
codefluent
tableview
channel
sipp
multi-dimensional-scaling
miniprofiler
django-import-export
boolean-logic
matlab-app-designer
cucumber-junit
jfxtras
love2d
djcelery
ms-dos
recovery
best-buy-api
spring-restcontroller
ipp-protocol
flume-twitter
dredd
phppgadmin
visual-format-language
sonatype
mifos
thread-exceptions
s-function
xcode8-beta4
seadragon
servicestack-bsd
html.actionlink
screen-orientation
enaml
blockquote
wlanapi
uitest
modern.ie
mobilefirst-server
angular-amd
explain
flow-js
kognitio-wx2
ildasm
visual-studio-6
siena
nachos
reference-counting
commenting
saga
iirf
insertonsubmit
ax
objective-c-runtime
reentrancy
lynx
daap
audiotoolbox
word-frequency
pcspim
play2-mini
getopts
outlook-2007-addin
firephp
efs
error-detection

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App