How to order by FIELD with GROUP BY - mysql

I have a query like this:
SELECT
t.type,
SUM( t.external_account )
FROM
u_contracts c,
u_data u,
u_transactions t
WHERE
c.user_id = u.id
AND t.contract_id = c.id
AND t.nulled =0
AND DATE (c.init_date) < DATE (u.dead)
AND u.dead IS NOT NULL
AND t.type != 'repay'
GROUP BY
t.type;
Now this gives me the desired result, but now I want to add ORDER BY to it, but by specific field name like:
ORDER BY FIELD( t.type, 'type1', 'type2', ... )
But I can't insert that into the query. When I try like this:
SELECT
t.type,
SUM( t.external_account )
FROM
u_contracts c,
u_data u,
u_transactions t
WHERE
c.user_id = u.id
AND t.contract_id = c.id
AND t.nulled =0
AND DATE (c.init_date) < DATE (u.dead)
AND u.dead IS NOT NULL
AND t.type != 'repay'
ORDER BY
FIELD( t.type, 'initial', 'commision', 'overpay', 'penalty', 'penalty2' )
GROUP BY
t.type;
It gives my sql error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY t.type' at line 17
What is the proper way in doing this?

You just have the wrong order for group by and order by;
...
ORDER BY
FIELD( t.type, 'initial', 'commision', 'overpay', 'penalty', 'penalty2' )
GROUP BY
t.type;
Should be:
...
GROUP BY
t.type
ORDER BY
FIELD( t.type, 'initial', 'commision', 'overpay', 'penalty', 'penalty2' );

Related

GROUP BY in SQL query is giving syntax error when used with Order By

I have this query:
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
ORDER BY message.sent_time DESC
GROUP BY customer.cus_id ;
where 31, 26 are the cus_id of the customers whose last messages I want to fetch.I am getting all the messages sorted asscording to time between the 1 and ( 31,26 ) customer if I remove GROUP BY constraint.
But adding it gives me this syntax error
[Error code:1064 SQL state:42000] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY customer.cus_id' at line 11
I have disabled ONLY_FULL_GROUP_BY SQL mode.
Still I dont know How to fetch the last message.
This is not an aggregation operations, this is a filtering operations.
Use window functions:
SELECT cm.* FROM (
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time,
ROW_NUMBER() OVER (PARTITION BY customer.cus_id ORDER BY message.sent_time DESC) as seqnum
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
) cm
WHERE seqnum = 1
ORDER BY msg_id DESC;
Group by clause will be before order by clause
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
GROUP BY customer.cus_id
ORDER BY message.msg_id DESC

Mysql sub-query return null

Hi I am a beginner I am trying to make all the result row of my subquery that is null to return 0 not null. but I am getting an error. I will really appreciate any advice. Thank you
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS 'Income'
SELECT
COUNT(DISTINCT t1.id) AS 'Val1',
COALESCE((SELECT SUM(CAST(COALESCE(r.t_payment_total,0) AS DECIMAL(18,2))) AS 'Income'
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status!="Pending" && r.t_status!="Booked" AND r.c_mid = m.id AND t.t_type_id = t1.t_type_id
),0)AS 'Income'
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id AND mp.t_type = 'try' AND mp.t_system_id = 'ok'
GROUP BY t1.t_tool_type_id
ORDER BY m.t_m ASC, tt.t_ttype ASC, mf.t_floor ASC;
Output I removed COALESCE I am having null
Val1 Income
10 Null
2 30
23 10
5 Null
Desired Output
Val1 Income
10 0
2 30
23 10
5 0
I would suggest moving that subquery into the from clause
SELECT
t1.t_tool_type_id
, COUNT( DISTINCT t1.id ) AS `Val1`
, COALESCE(i.Income , 0 ) AS `Income`
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id
AND mp.t_type = 'try'
AND mp.t_system_id = 'ok'
left join (
SELECT
CAST( SUM(r.t_payment_total) AS decimal(18, 2) ) AS `Income`
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status != 'Pending'
AND r.t_status != 'Booked'
AND r.c_mid = m.id
GROUP BY
t.t_type_id
) as i on t1.t_type_id = i.t_type_id
GROUP BY
t1.t_tool_type_id
;
nb: I have assumed r.t_payment_total is numeric.
I also suggest you always use single quotes for values/literals, and in MySQL backticks for identity (e.g. column headings) but really they are not essential in this query.

Mysql error in order by desc

this is my query.when i add order by desc in this query its gets an error pls help me.
SELECT *
FROM
(SELECT package_details.*,
r.state AS source_name,
d.state AS dest_name
FROM (`package_details`)
LEFT JOIN country_state_city r ON r.id=package_details.region_id
LEFT JOIN country_state_city d ON d.id=package_details.destination_id
WHERE `package_availability_type` = 'all'
AND `admin_status` = 'ACTIVE'
AND `status` = 'ACTIVE'
AND (package_name LIKE '%Istanbul%'
OR routes LIKE '%Istanbul%'
OR r.state LIKE '%Istanbul%'
OR d.state LIKE '%Istanbul%')
AND
ORDER BY `package_price` DESC
UNION ALL SELECT package_details.*,
r.state AS source_name,
d.state AS dest_name
FROM (`package_details`)
LEFT JOIN country_state_city r ON r.id=package_details.region_id
LEFT JOIN country_state_city d ON d.id=package_details.destination_id
WHERE `package_availability_type` ='range'
AND `admin_status` = 'ACTIVE'
AND `status` = 'ACTIVE'
AND `to_date` >= '2017-02-07'
AND (package_name LIKE '%Istanbul%'
OR routes LIKE '%Istanbul%'
OR r.state LIKE '%Istanbul%'
OR d.state LIKE '%Istanbul%')
AND
ORDER BY `package_price` DESC) AS dt LIMIT 0,100
error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY package_price DESC UNION ALL SELECT package_details.*,r.state AS sour' at line 1
Remove AND Before Order By Clause
AND
ORDER BY `package_price` DESC

mysql query Join table doesn't work

here is my query that makes an error for Join
$query = 'SELECT
a.ks_u_id,
a.ks_keyword,
b.u_photo
FROM
'.T_KEYWORD_HISTORY.' a
WHERE a.ks_u_id in ( SELECT uf_target_id FROM '.T_USER_FOLLOW.' WHERE uf_user_id="'.$u_id.'" and uf_target_id <> "'.$u_id.'" )
JOIN '.T_USER_ACCOUNT.' b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5 ';
I get this error message.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id ORDER BY a.ks_time DESC ' at line 8
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5
It looks like I wrote wrong query for JOIN command.
Try
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5
The JOIN clause comes before the WHERE clause. Your query should be:
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5

using SELECT inside INSERT query

please can anyone spot what is wrong with this query.It is giving error but i can't really see where the error is from since it is not really specified
mysql>
insert into sorting (mindlrid,maxdlrid)
values (
(select min(dlrid)
from dlr
where sid=
(select distinct(s.sendid)
from sent s
,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1))
,(select max(dlrid)
from dlr
where sid=(
select distinct(s.sendid)
from sent s,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1)
);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
You're missing a closing parenthesis. Probably at the very end of the insert. Equivalent: there is an extra opening paren on the values clause.
INSERT INTO sorting
(mindlrid,
maxdlrid)
VALUES (SELECT Min(dlrid)
FROM dlr
WHERE sid = (SELECT DISTINCT( s.sendid )
FROM sent s,
dlr d
WHERE s.uid = d.uid
AND d.d_billed = 1
AND d.d_sent = 0
AND s.processed = 1
AND s.sendid = d.sid
ORDER BY s.sendid ASC
LIMIT 1)),
(SELECT Max(dlrid)
FROM dlr
WHERE sid = (SELECT DISTINCT( s.sendid )
FROM sent s,
dlr d
WHERE s.uid = d.uid
AND d.d_billed = 1
AND d.d_sent = 0
AND s.processed = 1
AND s.sendid = d.sid
ORDER BY s.sendid ASC
LIMIT 1));
You can use the result of a SELECT directly, no need to wrap that into a VALUES clause:
insert into sorting (mindlrid,maxdlrid)
select min(dlrid), max(dlrid)
from dlr
where sid=
(select distinct s.sendid
from sent s
,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1)
Note that DISTINCT is not a function. It's an operator that includes all columns of the SELECT list.