MySQL Rows Not being ignored - mysql

I have the result returning the way I want - I just want to ignore rows where MPG_VALUE > 30 (nvdtechnical.TECH_Value_Float)
But my query returns all - and unwanted rows have NULL in MPG_VALUE column.
How can I return only rows where MPG_VALUE > 30 ?
SELECT *, (SELECT PR_Basic + PR_VAT + PR_Delivery FROM nvdprices
WHERE PR_Id = capder.cder_ID ORDER BY PR_EffectiveTo ASC LIMIT 1) as P11D,
(SELECT nvdtechnical.TECH_Value_Float FROM nvdtechnical
WHERE TECH_TechCode = '11'
AND nvdtechnical.TECH_Id = capder.cder_ID
AND nvdtechnical.TECH_Value_Float > 30
LIMIT 1) as MPG_VALUE
from capmod
JOIN capder ON capder.cder_modcode = capmod.cmod_code
JOIN caprange ON capder.cder_rancode = caprange.cran_code
JOIN capman ON caprange.cran_mantextcode = capman.cman_code
JOIN nvdmodelyear ON capder.cder_ID = MY_Id
AND nvdmodelyear.MY_EffectiveTo = '0000-00-00 00:00:00'
JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
AND nvddictionarycategory.DC_CatCode=4
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float >= '255'
JOIN capfueltype ON capfueltype.cft_code = capder.cder_fueltype
JOIN nvdbodystyle ON capmod.cmod_bodystyle = nvdbodystyle.bs_code
WHERE caprange.cran_mantextcode='140'
AND caprange.cran_code='522'
AND capder.cder_fueltype='P'
AND capder.cder_transmission='A'
AND nvdbodystyle.bs_code='3'
AND (cmod_discontinued=0 OR cmod_discontinued=2015)
AND capman.cman_code IN ('1','140','164')
AND caprange.cran_code IN ('924','126','147','955','965','661')
ORDER BY P11D, capman.cman_name ASC, caprange.cran_name, capmod.cmod_name

That is because you are doing a scalar query for the MPG Value. You are not really filtering which rows to show or hide, rather, you are showing all rows that has a value matched from your main query.
Imagine a left join, that is like what you are doing
SELECT nvdtechnical.TECH_Value_Float FROM nvdtechnical
WHERE TECH_TechCode = '11'
AND nvdtechnical.TECH_Id = capder.cder_ID
**AND nvdtechnical.TECH_Value_Float > 30**
LIMIT 1
to properly filter the data, you must filter in once more outside it in your where condition:
the easiest way would be like this one.
SELECT *
FROM (
SELECT *, (SELECT PR_Basic + PR_VAT + PR_Delivery FROM nvdprices
WHERE PR_Id = capder.cder_ID ORDER BY PR_EffectiveTo ASC LIMIT 1) as P11D,
(SELECT nvdtechnical.TECH_Value_Float FROM nvdtechnical
WHERE TECH_TechCode = '11'
AND nvdtechnical.TECH_Id = capder.cder_ID
AND nvdtechnical.TECH_Value_Float > 30
LIMIT 1) as **MPG_VALUE**
from capmod
JOIN capder ON capder.cder_modcode = capmod.cmod_code
JOIN caprange ON capder.cder_rancode = caprange.cran_code
JOIN capman ON caprange.cran_mantextcode = capman.cman_code
JOIN nvdmodelyear ON capder.cder_ID = MY_Id
AND nvdmodelyear.MY_EffectiveTo = '0000-00-00 00:00:00'
JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
AND nvddictionarycategory.DC_CatCode=4
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float >= '255'
JOIN capfueltype ON capfueltype.cft_code = capder.cder_fueltype
JOIN nvdbodystyle ON capmod.cmod_bodystyle = nvdbodystyle.bs_code
WHERE caprange.cran_mantextcode='140'
AND caprange.cran_code='522'
AND capder.cder_fueltype='P'
AND capder.cder_transmission='A'
AND nvdbodystyle.bs_code='3'
AND (cmod_discontinued=0 OR cmod_discontinued=2015)
AND capman.cman_code IN ('1','140','164')
AND caprange.cran_code IN ('924','126','147','955','965','661')
ORDER BY P11D, capman.cman_name ASC, caprange.cran_name, capmod.cmod_name
) x
WHERE x.MPG_VALUE IS NOT NULL;
Note that the x.MPG_VALUE which is filtered outside is your scalar query.
Edit: You can try other methods like, inner join.
SELECT *, (SELECT PR_Basic + PR_VAT + PR_Delivery FROM nvdprices
WHERE PR_Id = capder.cder_ID ORDER BY PR_EffectiveTo ASC LIMIT 1) as P11D,
MPG_VALUE.TECH_Id
from capmod
JOIN capder ON capder.cder_modcode = capmod.cmod_code
JOIN caprange ON capder.cder_rancode = caprange.cran_code
JOIN capman ON caprange.cran_mantextcode = capman.cman_code
JOIN nvdmodelyear ON capder.cder_ID = MY_Id
AND nvdmodelyear.MY_EffectiveTo = '0000-00-00 00:00:00'
JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
AND nvddictionarycategory.DC_CatCode=4
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float >= '255'
JOIN capfueltype ON capfueltype.cft_code = capder.cder_fueltype
JOIN nvdbodystyle ON capmod.cmod_bodystyle = nvdbodystyle.bs_code
INNER JOIN
(
SELECT TECH_Value_Float,TECH_Id
FROM nvdtechnical
WHERE TECH_TechCode = '11'
AND TECH_Value_Float > 30
) MPG_VALUE
ON MPG_VALUE.TECH_Id = capder.cder_ID
WHERE caprange.cran_mantextcode='140'
AND caprange.cran_code='522'
AND capder.cder_fueltype='P'
AND capder.cder_transmission='A'
AND nvdbodystyle.bs_code='3'
AND (cmod_discontinued=0 OR cmod_discontinued=2015)
AND capman.cman_code IN ('1','140','164')
AND caprange.cran_code IN ('924','126','147','955','965','661')
ORDER BY P11D, capman.cman_name ASC, caprange.cran_name, capmod.cmod_name;

Related

subquery doesn't seem to work

mysql query:
`SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`,` `a.`lastreplied`,`
a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id
and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON
`a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c`
ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN
(select cm.`conversation_id`, max(cm.`message_id`) as `message_id` from
`jos_social_conversations_message_maps` as cm
inner join `jos_social_conversations_message` as bm
on cm.`message_id` = bm.`id`
LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id`
AND `bus`.`target_id` = '602'
WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg
FROM jos_social_conversations_message_maps as maps WHERE a.id = maps.conversation_id AND
maps.isread = 0 AND maps.user_id = '602') AND `cm`.`state` = '1' and `bus`.`id` IS NULL
group by cm.`conversation_id`) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users`
as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
AND bus.`id` IS NULL AND `c`.`state` = '1'
AND this gives error like:
1054 - Unknown column 'a.id' in 'where clause'
subquery from above query as below:
AND(SELECT count(isread) AS newMsg FROM jos_social_conversations_message_maps WHERE conversation_id = 3 AND isread = 0 AND user_id = '602')
Current output:
Expected output:
There should be another column called 'newMsg' displaying total count of 'isread' column which has '0' as value for each id..
Here's my formatted version:
SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON `a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c` ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN (select cm.`conversation_id`, max(cm.`message_id`) as `message_id`
from `jos_social_conversations_message_maps` as cm
inner join `jos_social_conversations_message` as bm on cm.`message_id` = bm.`id`
LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id` AND `bus`.`target_id` = '602'
WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg
FROM jos_social_conversations_message_maps
WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
)
AND `cm`.`state` = '1'
and `bus`.`id` IS NULL
group by cm.`conversation_id`
) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users` as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
AND bus.`id` IS NULL AND `c`.`state` = '1'
Your subquery is part of the WHERE clause, so it will not return another column. Perhaps you are lookig for something like this:
SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`,
(SELECT count(isread)
FROM jos_social_conversations_message_maps
WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
) AS newMsg
FROM `jos_social_conversations` AS `a`
{remainder removed for brevity}

How to create codeigniter query from below sql query?

I've a query like this which is working fine but I want to create a pure CI query from this. I'm new in CI so can anyone help me to figure out how this can be done or suggest me a reference website or link from where i can get help. Thanks
SELECT
user_des,
IFNULL((sent),0) as sent,
IFNULL((viewed),0) as viewed,
CONCAT(IFNULL(fname_usr,user_des),' ',lname_usr,user_des) as fullname,
IFNULL(sum(duration),0) as totalviewtime,
IFNULL((totalviews),0) as views,
IFNULL(sum(sharedcount),0) as shares,
IFNULL(sum(shared),0) as shared
FROM dem_senddemo
INNER JOIN
(SELECT * FROM dem_demos WHERE id_dem IN (746,943,748) AND customer_dem = '1') as demosfiltered
ON demo_des = id_dem
LEFT JOIN
(
(
Select senddemo_wis, sum(duration) as duration, max(datereceived_wis) as datereceived_wis
FROM
(
select senddemo_wis, invited_wis, sum(IFNULL(duration_fea,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM (sta_views
LEFT JOIN sta_featureswisitavisits as s ON visit_fvi = id_vis
LEFT JOIN
(
(SELECT shortvideovid_fea as videoid_fea, shortvideoduration_fea as duration_fea
FROM dem_features
where shortvideoduration_fea > 0)
UNION
(SELECT longvideovid_fea as videoid_fea, longvideoduration_fea as duration_fea
FROM dem_features
where longvideoduration_fea > 0)
) as x
ON videoid_fea = showedvideo_fvi)
LEFT JOIN
sta_wistia as w ON invited_fvi = invited_wis AND s.showedvideo_fvi = w.mediaid_wis AND wistia_vis = email_wis
WHERE countvisit_wis <> 'N'
GROUP BY invited_wis
) as durfea
GROUP BY senddemo_wis
) UNION
(
SELECT senddemo_wis,sum(IFNULL(mainvideoduration_dem ,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM sta_wistia as w
INNER JOIN
(select *, customer_dem as cuss FROM dem_demos) as demcorp ON demo_wis = id_dem AND mainmessagingvideovid_dem = w.mediaid_wis
LEFT JOIN sta_views ON email_wis = wistia_vis
WHERE customer_dem = 1 AND senddemo_wis > 0 AND countvisit_wis <> 'N'
GROUP BY senddemo_wis
)
) as c ON id_des = senddemo_wis
LEFT JOIN
(
SELECT user_des as user2_des, count(id_vis) as totalviews
FROM sta_views
LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748)
GROUP BY user_des
) as z ON user_des = user2_des
LEFT JOIN
(
SELECT user_des as user3_des, count(id_des) as sent
FROM dem_senddemo
LEFT JOIN dem_invited ON id_senddemo = id_des
WHERE createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748) AND first_invited = 'YES'
GROUP BY user_des
) as za ON user_des = user3_des
LEFT JOIN
(
SELECT user_des as user4_des, count(id_vis) as viewed
FROM sta_views LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND state = 'VIEWED'
AND first_invited = 'YES' AND demo_des IN (746,943,748)
GROUP BY user_des
) as zb ON user_des = user4_des
LEFT JOIN
(
SELECT id_senddemo as iddemdemos2, count(id_senddemo) as shared, allshares as sharedcount
FROM
(
SELECT id_senddemo, count(id_senddemo) as allshares
FROM dem_invited
LEFT JOIN dem_senddemo on id_senddemo = id_des
WHERE first_invited <> 'YES' AND demo_des IN (746,943,748)
GROUP BY id_senddemo
) as x
GROUP BY id_senddemo
) as zc ON iddemdemos2 = id_des
LEFT JOIN
sec_users ON user_des = id_usr
WHERE customer_dem = 1 AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'
GROUP BY user_des

How do I merge these 2 MySQL queries

I have 2 queries, that are almost exactly the same.
Query #1:
SELECT
cman_code, cman_name
FROM
capder
JOIN
caprange ON capder.cder_rancode = caprange.cran_code
JOIN
capman ON caprange.cran_mantextcode = capman.cman_code
JOIN
capmod ON capder.cder_modcode = capmod.cmod_code
JOIN
nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN
nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN
nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
JOIN
nvdmodelyear ON capder.cder_ID = MY_Id
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float < '100'
AND nvdtechnical.TECH_Value_Float != 0
AND cder_transmission = 'M'
AND cman_code IN ('1','140','164','836','869','1283','1333','1925','2193','2514','4256','4537','4721','4835','5088','5171','5182','5238','5261','5509','6058','6425','7104','7983','8083','8219','9909','10172','10251','10383','10519','10968','12243','12764','14786','15155','21223','22439','44394','47841')
AND (cmod_discontinued=0 OR cmod_discontinued=2015)
AND cder_discontinued = '0000-00-00 00:00:00'
GROUP BY
cman_code
ORDER BY
cman_name DESC
Query #2:
SELECT
cman_code, cman_name
FROM
capder
JOIN
caprange ON capder.cder_rancode = caprange.cran_code
JOIN
capman ON caprange.cran_mantextcode = capman.cman_code
JOIN
capmod ON capder.cder_modcode = capmod.cmod_code
JOIN
nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN
nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN
nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
JOIN
nvdmodelyear ON capder.cder_ID = MY_Id
WHERE
nvddictionarytechnical.DT_TechCode = 11
AND nvdtechnical.TECH_Value_Float < 25
AND nvdtechnical.TECH_Value_Float != 0
AND cder_transmission = 'M'
AND cman_code IN ('1','140','164','836','869','1283','1333','1925','2193','2514','4256','4537','4721','4835','5088','5171','5182','5238','5261','5509','6058','6425','7104','7983','8083','8219','9909','10172','10251','10383','10519','10968','12243','12764','14786','15155','21223','22439','44394','47841')
AND (cmod_discontinued=0 OR cmod_discontinued=2015)
AND cder_discontinued = '0000-00-00 00:00:00'
GROUP BY
cman_code
ORDER BY
cman_name DESC
Both queries work perfectly and produce the exact results what I want.
The only difference between the two is the values in nvddictionarytechnical.**DT_TechCode** and nvdtechnical.**TECH_Value_Float**
I am having trouble bringing them together.
The - TECH_Value_Float - column, contains both CO2 and MPG values.
I just need to merge these into 1 query.
Using this does not work:
where
(
(
nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float < '100'
)
or
(
nvddictionarytechnical.DT_TechCode = 11
AND nvdtechnical.TECH_Value_Float < 25
)
)
The where and or produce results like:
|CO2 | MPG|
-----------
94 | 78 |
378 | 19 |
I need for the MPG and CO2 to both be true.
And results like:
|CO2 | MPG|
-----------
15 | 18 |
10 | 17 |
Any advice from the pros? Should I just make 2 queries? Has anyone ever come across a problem like this? Is this very unusual?
Thanks for your help.
Have you tried:
where
(
(
nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float < '100'
)
or
(
nvddictionarytechnical.DT_TechCode = 11
AND nvdtechnical.TECH_Value_Float < 25
)
)
What about this?
SELECT cman_code, cman_name
from capder
JOIN caprange ON capder.cder_rancode = caprange.cran_code
JOIN capman ON caprange.cran_mantextcode = capman.cman_code
JOIN capmod ON capder.cder_modcode = capmod.cmod_code
JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
JOIN nvdmodelyear ON capder.cder_ID = MY_Id
AND ((nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float < '100')
OR (nvddictionarytechnical.DT_TechCode = 11
AND nvdtechnical.TECH_Value_Float < 25 ))
AND nvdtechnical.TECH_Value_Float != 0
AND cder_transmission = 'M'
AND cman_code IN ('1','140','164','836','869','1283','1333','1925','2193','2514','4256','4537','4721','4835','5088','5171','5182','5238','5261','5509','6058','6425','7104','7983','8083','8219','9909','10172','10251','10383','10519','10968','12243','12764','14786','15155','21223','22439','44394','47841')
AND (cmod_discontinued=0 OR cmod_discontinued=2015)
AND cder_discontinued = '0000-00-00 00:00:00'
GROUP BY cman_code ORDER BY cman_name DESC
That way you check for both of the conditions!

How to optimize for speed a sql multiple select with SUM

I have a really long select from my database with many joins. The problem is with counting SUM: without sum, select time is about 3s, but with SUM is about 15s.
Is it possible to optimize my select to obtain a shorter select time?
Here is my code:
SELECT
accomodation.id,
accomodation.aid,
accomodation.title_en,
accomodation.title_url_en,
accomodation.address,
accomodation.zip,
accomodation.stars,
accomodation.picture,
accomodation.valid_from,
accomodation.valid_to,
accomodation.latitude,
accomodation.longitude,
accomodation.city_id AS
accomodation_city_id,
db_cities.id AS city_id,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_en AS country_title,
db_countries.title_url_en AS country_url,
accomodation_type.class AS accomodation_type_class,
accomodation_review_value_total.value AS review_total,
MIN(accomodation_price.price) AS price_from,
accomodation_rooms.total_persons
FROM
(SELECT aid, MAX(info_date_add) AS max_info_date_add FROM accomodation GROUP BY aid) accomodation_max
INNER JOIN accomodation
ON
accomodation_max.aid = accomodation.aid AND
accomodation_max.max_info_date_add = accomodation.info_date_add
LEFT JOIN db_cities
ON (
db_cities.id = accomodation.city_id OR
(((acos(sin((db_cities.latitude*pi()/180)) * sin((accomodation.latitude*pi()/180)) + cos((db_cities.latitude*pi()/180)) * cos((accomodation.latitude*pi()/180)) * cos(((db_cities.longitude - accomodation.longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) < '20')
JOIN db_countries
ON db_countries.id = accomodation.country_id
LEFT JOIN accomodation_review_value_total
ON accomodation_review_value_total.accomodation_aid = accomodation.aid
LEFT JOIN accomodation_type_value
ON accomodation_type_value.accomodation_id = accomodation.id
LEFT JOIN accomodation_type
ON accomodation_type.id = accomodation_type_value.accomodation_type_id
JOIN accomodation_season
ON (
accomodation_season.accomodation_aid = accomodation.aid AND
( '2013-11-04' BETWEEN accomodation_season.start_date AND accomodation_season.end_date OR '2013-11-05' BETWEEN accomodation_season.start_date AND accomodation_season.end_date ) )
JOIN accomodation_price
ON
accomodation_price.accomodation_aid = accomodation.aid AND
accomodation_price.accomodation_price_type_id = '1' AND
accomodation_price.accomodation_price_cat_id = '1' AND
accomodation_price.price BETWEEN '20' AND '250' AND
accomodation_price.accomodation_season_id = accomodation_season.id
JOIN accomodation_theme_value
ON accomodation_theme_value.accomodation_id = accomodation.id
INNER JOIN
(SELECT
accomodation_id,
SUM(accomodation_rooms.rooms) AS total_rooms,
SUM(accomodation_rooms.beds * accomodation_rooms.rooms) AS total_persons
FROM accomodation_rooms
GROUP BY accomodation_id) accomodation_rooms
ON
accomodation_rooms.accomodation_id = accomodation.id AND
accomodation_rooms.total_persons >= '4'
WHERE
db_countries.title_url_en LIKE '%spain%' AND
db_cities.title_url LIKE '%barcelona%' AND
accomodation_type_value.accomodation_type_id IN (5,10) AND
total_rooms >= '2' AND
accomodation_theme_value.accomodation_theme_id IN (11,12,13) AND
accomodation.stars IN (3,4,5) AND
( accomodation_review_value_total.value >= '4.5' ) AND
db_cities.id = '2416'
GROUP BY accomodation.aid
ORDER BY
CASE
WHEN accomodation.valid_to>=NOW() AND accomodation.valid_from<=NOW() AND MIN(accomodation_price.price) IS NOT NULL THEN 0
WHEN NOW()>accomodation.valid_to AND accomodation.valid_to>'0000-00-00' AND MIN(accomodation_price.price) IS NOT NULL THEN 1
WHEN accomodation.valid_to>=NOW() AND accomodation.valid_from<=NOW() THEN 2
WHEN NOW()>accomodation.valid_to AND accomodation.valid_to>'0000-00-00' THEN 3
ELSE 4 END,
review_total DESC,
accomodation.title_en
LIMIT 10

MySQL Query depending on column value define where statement

I have this SQL:
SELECT
`refnumbers`.`order_id`,
`refnumbers`.`deal_id`,
`refnumbers`.`claim_track_id`,
`deals`.`partner_count`
FROM `refnumbers`
JOIN `deals`
ON (`refnumbers`.`deal_id` = `deals`.`ID`)
JOIN `users`
ON (`users`.`id` = `deals`.`partner_id`)
WHERE `refnumbers`.`is_claimed` = '1'
AND `deals`.`partner_id` = '62039'
AND `refnumbers`.`claimed_at` BETWEEN '2013-01-17 00:00:00'
AND '2013-01-17 23:59:59'
ORDER BY `refnumbers`.`claimed_at` DESC
I would like to select where deals.partner_id = 62039 IF deals.partner_count is 1.
If partner_count isnt 1 then it should select where refnumbers.claim_track_id = 62039
How can this be done?
try
where
case when deals.partner_count = 1
then deals.partner_id = 62039
else refnumbers.claim_track_id = 62039
end
You need to have a condition enclose in a parenthesis which uses OR
SELECT `refnumbers`.`order_id`,
`refnumbers`.`deal_id`,
`refnumbers`.`claim_track_id`,
`deals`.`partner_count`
FROM `refnumbers`
INNER JOIN `deals`
ON (`refnumbers`.`deal_id` = `deals`.`ID`)
INNER JOIN `users`
ON (`users`.`id` = `deals`.`partner_id`)
WHERE `refnumbers`.`is_claimed` = '1' AND
`refnumbers`.`claimed_at` BETWEEN '2013-01-17 00:00:00' AND '2013-01-17 23:59:59'
AND
(
(deals.partner_id = 62039 AND deals.partner_count = 1)
OR
(refnumbers.claim_track_id = 62039 AND deals.partner_count <> 1)
)
ORDER BY `refnumbers`.`claimed_at` DESC
Try this:
SELECT r.order_id, r.deal_id,r.claim_track_id, d.partner_count
FROM refnumbers r
INNER JOIN deals d ON r.deal_id = d.ID
INNER JOIN users u ON d.partner_id = u.id
WHERE IF(d.partner_count = 1, d.partner_id = '62039', r.claim_track_id = 62039) AND
r.is_claimed = '1' AND r.claimed_at BETWEEN '2013-01-17 00:00:00' AND '2013-01-17 23:59:59'
ORDER BY r.claimed_at DESC