How do I merge these 2 MySQL queries - mysql

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!

Related

Slow Mysql Inner joins with multiple OR

I'm helping a friend with an e-commerce site. He has options for users to select different colours, styles, use and type of the products he's selling. The query the adds the following to the query:
INNER JOIN tbl_coloursProducts col ON ( p.product_id = col.productID AND (col.colourID = 2 OR col.colourID = 3 OR col.colourID = 5 OR col.colourID = 8 OR col.colourID = 10))
INNER JOIN tbl_useProducts tbluse ON ( p.product_id = tbluse.productID AND (tbluse.useID = 15 OR tbluse.useID = 16 OR tbluse.useID = 17 OR tbluse.useID = 18))
INNER JOIN tbl_styleProducts style ON ( p.product_id = style.productID AND (style.styleID = 39 OR style.styleID = 44))
INNER JOIN tbl_typeProducts type ON ( p.product_id = type.productID AND (type.typeID = 46 OR type.typeID = 48 OR type.typeID = 50))
The query loads fast enough when only a few options are selecting, but some users are selecting multiple or each which is causing the query to run in excess of 30 seconds and timing out.
Without altering the table structure is there a better way to optimise the query?
This is the full query:
SELECT *,
p.product_id,
Coalesce((SELECT p2sp.price
FROM ab_product_specials p2sp
WHERE p2sp.product_id = p.product_id
AND p2sp.customer_group_id = '1'
AND ( ( p2sp.date_start = '0000-00-00'
OR p2sp.date_start < Now() )
AND ( p2sp.date_end = '0000-00-00'
OR p2sp.date_end > Now() ) )
ORDER BY p2sp.priority ASC,
p2sp.price ASC
LIMIT 1), p.price) AS final_price,
pd.name AS name,
m.name AS manufacturer,
ss.name AS stock,
(SELECT Avg(r.rating)
FROM ab_reviews r
WHERE p.product_id = r.product_id
GROUP BY r.product_id) AS rating,
(SELECT Count(rw.review_id)
FROM ab_reviews rw
WHERE p.product_id = rw.product_id
GROUP BY rw.product_id) AS review
FROM ab_products p
LEFT JOIN ab_product_descriptions pd
ON ( p.product_id = pd.product_id
AND pd.language_id = '1' )
LEFT JOIN ab_products_to_stores p2s
ON ( p.product_id = p2s.product_id )
LEFT JOIN ab_manufacturers m
ON ( p.manufacturer_id = m.manufacturer_id )
LEFT JOIN ab_stock_statuses ss
ON ( p.stock_status_id = ss.stock_status_id
AND ss.language_id = '1' )
LEFT JOIN ab_products_to_categories p2c
ON ( p.product_id = p2c.product_id )
INNER JOIN tbl_coloursproducts col
ON ( p.product_id = col.productid
AND ( col.colourid = 2
OR col.colourid = 3
OR col.colourid = 5
OR col.colourid = 8
OR col.colourid = 10 ) )
INNER JOIN tbl_useproducts tbluse
ON ( p.product_id = tbluse.productid
AND ( tbluse.useid = 15
OR tbluse.useid = 16
OR tbluse.useid = 17
OR tbluse.useid = 18 ) )
INNER JOIN tbl_styleproducts style
ON ( p.product_id = style.productid
AND ( style.styleid = 39
OR style.styleid = 44 ) )
INNER JOIN tbl_typeproducts type
ON ( p.product_id = type.productid
AND ( type.typeid = 46
OR type.typeid = 48
OR type.typeid = 50 ) )
WHERE p.status = '1'
AND p.date_available <= Now()
AND p2s.store_id = 0
AND p2c.category_id = 131
GROUP BY p.product_id
ORDER BY p.product_id DESC
LIMIT 0, 8
Without the custom bits the query runs fine.
Looking at that query, not sure the ORs are the problem themselves (although you could possibly make the code more compact by using and IN clause for each one). Rather I suspect that selecting more and more options results in more rows being returned. And this is causing problems with the sub queries in the SELECT clause.
Can you try the query with the sub queries removed from the SELECT clause and see the effect that has.
You can remove the sub queries quite easily.
SELECT *,
p.product_id,
Coalesce(sub1.price, p.price) AS final_price,
pd.name AS name,
m.name AS manufacturer,
ss.name AS stock,
sub0.rating,
sub0.review
FROM ab_products p
INNER JOIN
(
SELECT r.product_id,
Avg(r.rating) AS rating,
Count(rw.review_id) AS review
FROM ab_reviews r
GROUP BY r.product_id
) sub0
ON p.product_id = sub0.product_id
LEFT OUTER JOIN
(
SELECT p2sp.product_id,
SUBSTRING_INDEX(GROUP_CONCAT(p2sp.price ORDER BY p2sp.priority ASC, p2sp.price ASC ), ',', 1) AS price
FROM ab_product_specials p2sp
WHERE p2sp.customer_group_id = '1'
AND ( p2sp.date_start = '0000-00-00' OR p2sp.date_start < NOW() )
AND ( p2sp.date_end = '0000-00-00' OR p2sp.date_end > NOW() )
GROUP BY p2sp.product_id
) sub1
ON p.product_id = sub1.product_id
LEFT JOIN ab_product_descriptions pd
ON ( p.product_id = pd.product_id
AND pd.language_id = '1' )
LEFT JOIN ab_products_to_stores p2s
ON ( p.product_id = p2s.product_id )
LEFT JOIN ab_manufacturers m
ON ( p.manufacturer_id = m.manufacturer_id )
LEFT JOIN ab_stock_statuses ss
ON ( p.stock_status_id = ss.stock_status_id
AND ss.language_id = '1' )
LEFT JOIN ab_products_to_categories p2c
ON ( p.product_id = p2c.product_id )
INNER JOIN tbl_coloursproducts col
ON ( p.product_id = col.productid
AND ( col.colourid = 2
OR col.colourid = 3
OR col.colourid = 5
OR col.colourid = 8
OR col.colourid = 10 ) )
INNER JOIN tbl_useproducts tbluse
ON ( p.product_id = tbluse.productid
AND ( tbluse.useid = 15
OR tbluse.useid = 16
OR tbluse.useid = 17
OR tbluse.useid = 18 ) )
INNER JOIN tbl_styleproducts style
ON ( p.product_id = style.productid
AND ( style.styleid = 39
OR style.styleid = 44 ) )
INNER JOIN tbl_typeproducts type
ON ( p.product_id = type.productid
AND ( type.typeid = 46
OR type.typeid = 48
OR type.typeid = 50 ) )
WHERE p.status = '1'
AND p.date_available <= Now()
AND p2s.store_id = 0
AND p2c.category_id = 131
GROUP BY p.product_id
ORDER BY p.product_id DESC
LIMIT 0, 8
As an aside, when you read from ab_product_specials you are checking for the date_start and date_end to be 0000-00-00 (ie, dates), but also comparing them with NOW() which returns a date / time field. Are those fields date or date / time fields?
My first though was to use IN to make the query eaier to read:
INNER JOIN tbl_coloursProducts col
ON p.product_id = col.productID AND col.colourID IN ( 2, 3, 5, 8, 10 )
Then I thought, I wonder if they are dynamically building SQL text to squirt into the database logic?! The optimizer is unlikely to do well at optimizing queries when they are constantly mutating in this way.
Consider a scratch table (pseudo code):
-- One time:
CREATE TABLE SratchColours ( colourID INT NOT NULL UNQIUE );
-- For each query:
DELETE FROM SratchColours;
INSERT INTO SratchColours VALUES ( 2 ), ( 3 ), ( 5 ), ( 8 ), ( 10 );
Now you dynamic list of values simply becomes just another join:
tbl_coloursProducts NATURAL JOIN SratchColours
(or you could use an inner join if you must!)
Now, having one base table for every concurrent user is probably not a great way to scale a system. Therefore, consider how to pass a bag of colourID values to the database logic (say, a stored proc), put them into a table (say, a temporary table), then join from there to your base tables.

How to remove range check in query

I have the following query, when I do an explain for the query it says "Range checked for each record (index map: 0x8)". I am assuming this is one reason the query is as slow, how can I improve this
The query looks as follow currently,
explain select
*
from
(select
CASE WHEN dd.parentId IS NOT NULL
THEN CONCAT(dd.`profileId`,dd.`orgId`,dd.parentId)
ELSE CONCAT(dd.`profileId`,dd.`orgId`,dd.id) END as ticketId,dd.*
from
t_dtls_prod dd where dd.currentlyActive = true and dd.dataSource ='SAND' AND msgCreatedOn BETWEEN '2016-09-01 00:00:00'
AND '2016-10-03 00:00:00' and dd.profileId = 148 group by ticketId ) as d
left join basket_prod b on b.basketId=d.toBasketId
left join lead_social_accts_prod a on a.orgId=1002
AND (
(
(
(a.`type`='TWITTER' and d.channel in (1,34)) or (a.`type`='FACEBOOK' and d.channel in (6,7))
or (a.`type`='GOOGLEPLUS' and d.channel in (5,25)) or (a.`type`='LINKEDIN' and d.channel =30)
or (a.`type`='GOOGLEPLUS' and d.channel=36) or ((a.`type`='YOUTUBE' or a.`type`='GOOGLEPLUS') and d.channel=27)
or (a.`type`='TUMBLR' and d.channel in (29,31)) or
( a.`type`='INSTAGRAM' and d.channel=35)
)
AND
d.userChannelId=a.socialId) OR
(a.`type`='BLOG' and d.channel in (9,11,15,21) AND d.msgId=a.socialId))
left join lead_prod l on l.leadId=a.leadId and l.orgId=1002 where d.profileId = 148 limit 0,1000
The results seem to come out in a descent time upto 1000 rows, but beyond that it just dies.
This should be better
EXPLAIN SELECT
d.*, b.*, a.*, l.*,
(CASE WHEN d.parentId IS NOT NULL
THEN CONCAT(d.`profileId`, d.`orgId`, d.parentId)
ELSE CONCAT(d.`profileId`, d.`orgId`, d.id) END) as ticketId
FROM
t_dtls_prod d
LEFT JOIN basket_prod b ON b.basketId = d.toBasketId
LEFT JOIN lead_social_accts_prod a ON a.orgId = 1002 AND
(
(d.msgId = a.socialId AND d.channel in (9,11,15,21) AND a.`type`='BLOG')
OR
(
d.userChannelId = a.socialId
AND
(
(d.channel IN (1,34) AND a.`type`='TWITTER')
OR
(d.channel IN (6,7) AND a.`type`='FACEBOOK')
OR
(d.channel IN (5, 25, 36) AND a.`type`='GOOGLEPLUS')
OR
(d.channel = 30 AND a.`type`='LINKEDIN')
OR
(d.channel=27 AND a.`type` IN ('YOUTUBE', 'GOOGLEPLUS'))
OR
(d.channel IN (29,31) AND a.`type`='TUMBLR')
OR
(d.channel = 35 AND a.`type`='INSTAGRAM')
)
)
)
LEFT JOIN lead_prod l ON l.leadId = a.leadId AND l.orgId = 1002
WHERE
d.currentlyActive = true AND d.dataSource ='SAND' AND msgCreatedOn BETWEEN '2016-09-01 00:00:00' AND '2016-10-03 00:00:00' AND d.profileId = 148
GROUP BY ticketId
LIMIT 0,1000
LEFT JOIN lead_social_accts_prod a ON a.orgId = 1002 here you forgot connection to t_dtls_prod or basket_prod like a.someId = b.someId

MySQL Rows Not being ignored

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;

Join three completed tables in MySQL

I have three tables: tblFuel, tblDoXang, tblDrivingTime2. Now I want to show the fuelLevel field in tblFuel that satisfy some condition in tblDrivingTime2 and if in specific of time(base on timestamp in tblFuel) if I check there are adding fuel action in tblDoXang, I have to insert it(nhienLieu field in tblDoXang) to report that show fuelLevel above. The goal is likely:
tblFuel:
timestamp fuelLevel
123456 10
123467 8
123478 50
123489 20
tblDrivingTime2:
stopTime
123456
123478
123489
this will print:
10
50
20
and now we check if
tblDoXang
thoiGian nhienLieu
123457 15
123466 10
it will insert to result above and finally, the result will be:
10
15
10
50
20
I have written two separated queries to do these tasks:
SELECT distinct from_unixtime(F.timestamp), F.fuelLevel FROM gtse.tblFuel F
INNER JOIN gtse.tblDrivingTime2 D
ON D.accountID = F.accountID and D.deviceID = F.deviceID
where (from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02')
and F.accountID = 'vinhnghia'
and F.deviceID = '14C-00263'
and (D.reportType = '2' or D.reportType = '3')
and F.timestamp = D.stopTime
order by F.timestamp asc;
this will print the result in the first time(10,50 and 20) and this:
SELECT distinct from_unixtime(D.thoiGian), D.nhienLieu
FROM gtse.tblDoXang D
inner join gtse.tblFuel F
on D.accountID = F.accountID and D.deviceID = F.deviceID
where D.accountID = 'vinhnghia' and D.deviceID = '14C-00263'
and D.thoiGian <= F.timestamp
order by D.thoiGian asc;
will show the value(15,10). And my question is how to join two queries to show the final result?:
10
15
10
50
20
Maybe you could try with the UNION clause:
(
SELECT distinct
from_unixtime(F.timestamp) As col_1,
F.fuelLevel as col_2
FROM
gtse.tblFuel F
INNER JOIN gtse.tblDrivingTime2 D
ON D.accountID = F.accountID
and D.deviceID = F.deviceID
where
(from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02')
and F.accountID = 'vinhnghia'
and F.deviceID = '14C-00263'
and (D.reportType = '2' or D.reportType = '3')
and F.timestamp = D.stopTime
order by
F.timestamp asc
)
UNION
(
SELECT distinct
from_unixtime(D.thoiGian) as col_1,
D.nhienLieu as col_2
FROM
gtse.tblDoXang D
inner join gtse.tblFuel F
on D.accountID = F.accountID
and D.deviceID = F.deviceID
where
D.accountID = 'vinhnghia'
and D.deviceID = '14C-00263'
and D.thoiGian <= F.timestamp
order by
D.thoiGian asc;
)
I hope I understood well and this could help.

SQL Query - put multiple condition on one product

Please help me with this:
I have a table like:
id_feature id_product id_feature_value
1 1 50
2 1 54
5 1 67
And I want to select from this table like this:
select count(id_product) from table where (id_feature = 1 AND id_feature_value = 50) AND (id_feature = 2 AND id_feature_value = 54) AND (id_feature = 5 AND id_feature_value = 67)
my query must meet the conditions. Like having count(condition) = 3
I don't know how to write this!
Please help me! and sorry my english!
SELECT count( pf.id_product ) AS nr_product, value, fv.id_feature_value, filter
FROM `nk_category_features` cat_f
INNER JOIN `nk_feature_value` fv ON fv.id_feature = '11'
AND fv.value IS NOT NULL
INNER JOIN `nk_product_features` pf ON pf.id_feature = '11'
AND pf.id_feature_value = fv.id_feature_value
INNER JOIN `nk_product` p ON p.id_product = pf.id_product
AND p.product_active = '1'
INNER JOIN `nk_product_features` pf1 ON ( pf1.id_feature = '14'
AND (
pf1.id_feature_value = '21'
) )
WHERE cat_f.id_feature = '11'
AND filter >0
GROUP BY pf.id_feature, pf.id_product
ORDER BY abs( fv.value ) ASC
this is my query that i used now, but i don't like the solution width inner join, inner join on the same table
try this:
select id_product,count(*)
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
To get just the total of products meeting the where clause
select count(*)
from
( select id_product
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
) tmp