Order by on datetime field is not working - mysql - mysql

I want to get the date of last order placed and mobile# for all the customers.
This is my query.
SELECT c.customer_id, c.mobile, COALESCE( MAX( o.order_datetime ) , '0000-00-00 00:00:00' ) AS last_order_date
FROM table_orders AS o
RIGHT JOIN table_customers AS c ON o.customer_id = c.customer_id
GROUP BY c.customer_id
ORDER BY DATE( o.order_datetime )
It works fine. But not giving me data in asc order of o.order_datetime?
What is the error in query?

Try this
ORDER BY DATE( o.order_datetime ) DESC

Related

sql join Query the database ,There is no desired result,

my demand is:from database query newest data,according to time field as a standard,query alarmtime this field ,but alarmtime < time
executive sql:
SELECT
c.entryTime AS alarmtime ,
a.time AS time
FROM (
SELECT
t.DB33,
MAX( t.Time ) AS time,
t.Stream,
t.Coil,
t.`View`
FROM (
SELECT DB33, Time, Stream, Coil, `View`
FROM running_check
ORDER BY id DESC
LIMIT 1000 ) as t
GROUP BY t.DB33 ) AS a
LEFT JOIN (
select cameraID,cameraName
from monitor_link_info) as b ON a.db33 = b.cameraID
LEFT JOIN (
SELECT entryTime, cameraID
FROM result_video_v2
ORDER BY id DESC
limit 1000 ) AS c ON a.db33 = c.cameraId
GROUP BY a.db33, b.cameraName
ORDER BY a.time DESC, alarmtime DESC
execute result is unamiable,please look at image
now time column not what i want,Because he didn't change.
SELECT
c.entryTime AS alarmtime ,
a.time AS TIME
FROM
(
SELECT DB33, MAX(TIME) TIME, Stream, Coil, `View`
FROM running_check
ORDER BY id DESC
GROUP BY t.DB33
LIMIT 1000
) AS a
LEFT JOIN
(
SELECT cameraID,cameraName
FROM monitor_link_info
GROUP BY cameraID
LIMIT 1000
) AS b ON (a.db33 = b.cameraID)
LEFT JOIN (
SELECT entryTime, cameraID
FROM result_video_v2
GROUP BY cameraID
ORDER BY id DESC
LIMIT 1000
) AS c ON (a.db33 = c.cameraID)
GROUP BY a.db33, b.cameraName
ORDER BY a.time DESC, c.entryTime DESC

How to select customer's final balance for all customer in mysql

I have 3 tables.
table_customers - customer_id, name
table_orders - order_id, customer_id, order_datetime
table_wallet - customer_id, amount, type // type 1- credit, type 2- debit
I need to get all customers, their total balance, and their last order date and order id. This is my query.
SELECT
C.customer_id,
C.name,
COALESCE( SUM(CASE WHEN type = 2 THEN -W.amount ELSE W.amount END), 0) AS value,
COALESCE( max( O.order_id ) , '0' ) AS last_order_id,
COALESCE( max( date( O.order_datetime ) ) , '0000-00-00' ) AS last_order_date
FROM
table_customers as C
LEFT JOIN
table_wallet as W
ON C.customer_id = W.customer_id
LEFT JOIN
table_orders AS O
ON W.customer_id = O.customer_id
group by C.customer_id
ORDER BY C.customer_id
Everything is coming correct except customer's total value. From result it seems its getting added multiple times.
What is wrong in query? Can anyone help me on this?
This is doing a many-to-many join on table_customers to table_orders, which will mess with your sums. Rather do this:
SELECT C.customer_id
, C.name
, IFNULL((SELECT SUM(IF(W.type=2, -1*W.amount, W.amount))
FROM table_wallet W
WHERE C.customer_id = W.customer_id),0) AS value
, IFNULL((SELECT MAX(DATE(O.order_id))
FROM table_orders O
WHERE C.customer_id = O.customer_id),'0') AS last_order_id
, IFNULL((SELECT MAX(DATE(O.order_datetime))
FROM table_orders O
WHERE C.customer_id = O.customer_id),'0000-00-00') AS last_order_date
FROM table_customers as C
ORDER BY C.customer_id
This will return one row per customer, then subquery the fields you want. I've substituted IFNULL for COALESCE as I find it cleaner, but this is a preference thing.

MySQL SELECT subquery

I have a calendar and user_result table and I need to join these two queries.
calendar query
SELECT `week`, `date`, `time`, COUNT(*) as count
FROM `calendar`
WHERE `week` = 1
GROUP BY `date`
ORDER BY `date` DESC
and the result is
{"week":"1","date":"2014-08-21","time":"15:30:00","count":"4"}, {"week":"1","date":"2014-08-20","time":"17:30:00","count":"12"}
user_result query
SELECT `date`, SUM(`point`) as score
FROM `user_result`
WHERE `user_id` = 1
AND `date` = '2014-08-20'
and the result is just score 3
My goal is to always show calendar even if the user isn't present in the user_result table, but if he is, SUM his points for that day where calendar.date = user_result.date. Result should be:
{"week":"1","date":"2014-08-21","time":"15:30:00","count":"4","score":"3"}, {"week":"1","date":"2014-08-20","time":"17:30:00","count":"12","score":"0"}
I have tried this query below, but the result is just one row and unexpected count
SELECT c.`week`, c.`date`, c.`time`, COUNT(*) as count, SUM(p.`point`) as score
FROM `calendar` c
INNER JOIN `user_result` p ON c.`date` = p.`date`
WHERE c.`week` = 1
AND p.`user_id` = 1
GROUP BY c.`date`
ORDER BY c.`date` DESC
{"week":"1","date":"2014-08-20","time":"17:30:00","count":"4","score":"9"}
SQL Fiddle
ow sorry, i was edited, and i was try at your sqlfiddle, if you want to show all date from calendar you can use LEFT JOIN, but if you want to show just the same date between calendar and result you can use INNER JOIN, note: in this case INNER JOIN just show 1 result, and LEFT JOIN show 2 results
SELECT c.`week`, p.user_id, c.`date`, c.`time`, COUNT(*) as count, p.score
FROM `calendar` c
LEFT JOIN
(
SELECT `date`, SUM(`point`) score, user_id
FROM `result`
group by `date`
) p ON c.`date` = p.`date`
WHERE c.`week` = 1
GROUP BY c.`date`
ORDER BY c.`date` DESC
I put a pre-aggreate query / group by date as a select for the one person you were interested in... then did a left-join to it. Also, your column names of week, date and time (IMO) are poor choice column names as they can appear to be too close to reserved keywords in MySQL. They are not, but could be confusing..
SELECT
c.week,
c.date,
c.time,
coalesce( OnePerson.PointEntries, 0 ) as count,
coalesce( OnePerson.totPoints, 0 ) as score
FROM
calendar c
LEFT JOIN ( select
r.week,
r.date,
COUNT(*) as PointEntries,
SUM( r.point ) as totPoints
from
result r
where
r.week = 1
AND r.user_id = 1
group by
r.week,
r.date ) OnePerson
ON c.week = OnePerson.week
AND c.date = OnePerson.date
WHERE
c.week = 1
GROUP BY
c.date
ORDER BY
c.date DESC
Posted code to SQLFiddle

MySQL Query. Give closest date to present day

I have this query that gives me back more than one result. I would like to edit it so that it only will give me one result per o.Customer_ID and then have that be the order that would be closest to today with o.OrderPlaceServerTime.
Here's the Query I have:
SELECT
o.Order_ID,
o.Customer_ID,
o.OrderPlaceServerTime,
o.CustomerOrderTotal
FROM
Orders o
LEFT JOIN Order_LineDetails oln
ON oln.Order_ID = o.Order_ID
WHERE
o.OrderPlaceServerTime >= '2012-09-01 00:00:00'
AND o.OrderPlaceServerTime <= '2012-12-01 00:00:00'
AND o.CustomerOrderTotal >= '50'
AND oln.Product_ID = '75'
What would I need to change to achieve this?
I would like to edit it so that it only will give me one result per o.Customer_ID
So, first you should get the latest order for the qualifying Customer IDs, by grouping the qualifying order by customer ID and picking the max or OrderPlaceServerTime. Then you would join those records with the Orders on customer ID and the OrderPlaceServerTime to pick the other two attributes of interest.
SELECT
o.Order_ID,
o.Customer_ID,
o.OrderPlaceServerTime,
o.CustomerOrderTotal
FROM
Orders o
JOIN (
SELECT
o.Customer_ID,
MAX(o.OrderPlaceServiceTime) 'MaxOrderPlaceServiceTime'
FROM
Orders o
LEFT JOIN Order_LineDetails oln
ON oln.Order_ID = o.Order_ID
WHERE
o.OrderPlaceServerTime >= '2012-09-01 00:00:00'
AND o.OrderPlaceServerTime <= '2012-12-01 00:00:00'
AND o.CustomerOrderTotal >= '50'
AND oln.Product_ID = '75'
GROUP BY o.Customer_ID
) AS A
ON
o.Customer_ID = A.Customer_ID AND
o.OrderPlaceServiceTime = A.MaxOrderPlaceServiceTime
If I understood correctly what you need, this should be the query :
SELECT * FROM
( SELECT
o.Order_ID,
o.Customer_ID,
o.OrderPlaceServerTime,
o.CustomerOrderTotal
FROM
Orders o
LEFT JOIN Order_LineDetails oln
ON oln.Order_ID = o.Order_ID
WHERE
o.OrderPlaceServerTime >= '2012-09-01 00:00:00'
AND o.OrderPlaceServerTime <= '2012-12-01 00:00:00'
AND o.CustomerOrderTotal >= '50'
AND oln.Product_ID = '75'
ORDER BY o.Customer_ID, o.OrderPlaceServerTime DESC ) AS tab1
GROUP BY o.Customer_ID;

MySQL query not working with another database

I have this query which is working fine:
SELECT c.id, c.name
FROM job j
LEFT JOIN portfolio_job_category pjc ON pjc.job_id = j.id
LEFT JOIN category c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY `c`.`id` ASC
LIMIT 0 , 30
But when I do query below it doesn't work and returns empty rows:
SELECT c.id, c.name
FROM commstratjobs.job j
LEFT JOIN commstratjobs.portfolio_job_category pjc ON pjc.job_id = j.id
LEFT JOIN commstratjobs.category c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY c.name;
LIMIT 0 , 30
Is there a reason why?
Only thing I did was to add database in front of tables...
did you try to seprate database & table by this symbol : `
i.e
commstratjobs.job
so your query will be like this
SELECT c.id, c.name
FROM `commstratjobs`.`job j`
LEFT JOIN `commstratjobs`.`portfolio_job_category` pjc ON pjc.job_id = j.id
LEFT JOIN `commstratjobs`.`category` c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY c.name;
LIMIT 0 , 30