I am getting a syntax error can anybody tell me why?
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT TOP 1 previousbalance FROM invoice i
WHERE i.client = c.clientid ORDER BY i.invoiceid DESC) AS remaining
FROM client c
ORDER BY clientname
What the secondary select is doing is getting the latest record for that clientid in the invoice table.
The program - HediSQl
SQL
And here is the error:
SQL Error (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 '1 previousbalance FROM invoice i WHERE i.client = c.clientid ORDER BY i.invoicei' at line 1 */
Just guessing but it might indicate that you should replace TOP 1 with LIMIT 1 or WHERE ROWNUM < 2 LIMIT 1. What kind of DB are you using?
You just need to use LIMIT instead TOP like this:
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT previousbalance FROM invoice i
WHERE i.client = c.clientid ORDER BY i.invoiceid DESC LIMIT 1) AS remaining
FROM client c
ORDER BY clientname
See this SQLFiddle
The syntax you have will work in SQL Server. Your error message is from MySQL.
Try this:
SELECT c.clientid,
c.clientname,
c.billingdate,
(
SELECT previousbalance
FROM invoice i
WHERE i.client = c.clientid
ORDER BY i.invoiceid DESC
LIMIT 1
) AS remaining
FROM client c
ORDER BY clientname
Related
I tried ORDER BY and GROUP BY together on statement, and ORDER by is not working; this is the code: SELECT ter_code, ter_name, tgp_longitude, tgp_latitude FROM tblterminalgps, tblterminal WHERE ter_status != ? AND tgp_teruid = ter_uid GROUP BY ter_uid ORDER BY tgp_datecreated DESC
On second try I tried this one: SELECT t2.ter_code, t2.ter_name, t1.tgp_longitude, t1.tgp_latitude FROM tblterminalgps as t1 INNER JOIN tblterminal as t2 ON t1.tgp_teruid = t2.ter_uid
(SELECT * FROM tblterminalgps GROUP BY t1.tgp_teruid DESC)
ORDER BY tgp_datecreated DESC
and I am getting this kind of error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM tblterminalgps GROUP BY t1.tgp_teruid DESC)
ORDER BY tgp_datecrea' at line 2
everbody. I have prooblem with LIKE in IF construction. I have the following query SQL:
SELECT DISTINCT a.ID_SPORTSMAN, a.NAME, a.SURNAME, a.SEX, a.CLUB, b.RESULTS
FROM `sportsman` AS `a`
JOIN `results` AS `b`
ON a.ID_SPORTSMAN = b.ID_SPORTSMAN AND b.ID_DISCIPLINE = '1' ORDER BY b.RESULTS;
IF (b.RESULTS LIKE '`%')
THEN
ASC LIMIT 10
ELSE
DESC LIMIT 10
END IF
I usage MariaDB. In phpmyadmin return me this error:
Error
Static analysis:
1 errors were found during analysis.
Unrecognized statement type. (near "IF" at position 0)
SQL query:
IF (b.REZULTAT LIKE '`%') THEN ASC LIMIT 10 ELSE DESC LIMIT 10 END IF
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'ASC LIMIT 10 ELSE DESC LIMIT 10 END IF' at line 3
What is wrong?
You can try using case in the order by:
select distinct s.ID_SPORTSMAN,
s.name,
s.SURNAME,
s.SEX,
s.CLUB,
r.RESULTS
from sportsman as s
join results as r on s.ID_SPORTSMAN = r.ID_SPORTSMAN
where r.ID_DISCIPLINE = '1'
order by case
when r.RESULTS like '`%'
then r.RESULTS
end asc,
r.RESULTS desc limit 10;
You cannot use if like this. Start with this query:
SELECT s.ID_SPORTSMAN, s.NAME, s.SURNAME, s.SEX, s.CLUB, r.RESULTS
FROM sportsman s JOIN
results r
ON s.ID_SPORTSMAN = r.ID_SPORTSMAN AND r.ID_DISCIPLINE = 1
ORDER BY r.RESULTS ASC
LIMIT 10;
And then work from there.
Notes:
I removed SELECT DISTINCT because I doubt it is necessary. If it is, use the DISTINCT.
I replaced the table aliases with meaningful table abbreviations. That makes the query much easier to follow.
I simplified the ORDER BY clause so it should work.
I removed the single quotes around "1", because it is presumably an integer.
You can modify this to get what you really want.
EDIT:
Hmmm, I think you might want this:
(SELECT s.ID_SPORTSMAN, s.NAME, s.SURNAME, s.SEX, s.CLUB, r.RESULTS
FROM sportsman s JOIN
results r
ON s.ID_SPORTSMAN = r.ID_SPORTSMAN AND r.ID_DISCIPLINE = 1
WHERE s.RESULTS LIKE '`%'
ORDER BY r.RESULTS ASC
LIMIT 10
) UNION ALL
(SELECT s.ID_SPORTSMAN, s.NAME, s.SURNAME, s.SEX, s.CLUB, r.RESULTS
FROM sportsman s JOIN
results r
ON s.ID_SPORTSMAN = r.ID_SPORTSMAN AND r.ID_DISCIPLINE = 1
WHERE s.RESULTS NOT LIKE '`%'
ORDER BY r.RESULTS DESC
LIMIT 10
);
I need to make a sql that groups by three different columns, this is my query:
SELECT
notification.type,
notification.triggerer,
notification.to_what,
ANY_VALUE(user_data.name) AS name,
ANY_VALUE(user_data.surname) AS surname,
ANY_VALUE(user_data.avatarimage) AS avatarimage,
COUNT(*) AS counter
FROM notification
INNER JOIN user_data
ON notification.triggerer = user_data.owner
WHERE notification.owner = "c6cecc891f6c4cc84cc0b62062578e52"
AND isdelete=0
ORDER BY notification.id DESC
GROUP BY notification.triggerer, notification.type, notification.to_what
LIMIT 0,100
But when I make this, it shows me an error:
> #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
'GROUP BY notification.triggerer, notification.type, notification.to_what
LIMIT ' at line 15
How can I fix it?
You can use it, but your your group by must be before order by
SELECT
notification.type,
notification.triggerer,
notification.to_what,
ANY_VALUE(user_data.name) AS name,
ANY_VALUE(user_data.surname) AS surname,
ANY_VALUE(user_data.avatarimage) AS avatarimage,
COUNT(*) AS counter
FROM notification
INNER JOIN user_data
ON notification.triggerer = user_data.owner
WHERE notification.owner = "c6cecc891f6c4cc84cc0b62062578e52"
AND isdelete=0
GROUP BY notification.triggerer, notification.type, notification.to_what
ORDER BY notification.id DESC
LIMIT 0,100
Rediculous but I cant find error in this request
SELECT * FROM diploms
LEFT JOIN student ON diploms.student_id = student.student_id
LEFT JOIN group ON student.group_id = group.group_id
LEFT JOIN speciality ON group.speciality_id = speciality.speciality_id
ORDER BY (CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END), speciality.name ASC
But SQL says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group ON student.group_id = group.group_id LIMIT 0, 30' at line 3
WTH?
group is a reserved keyword in MySQL and needs to be escaped by backticks.
SELECT *
FROM diploms
LEFT JOIN student ON diploms.student_id = student.student_id
LEFT JOIN `group` ON student.group_id = `group`.group_id
LEFT JOIN speciality ON `group`.speciality_id = speciality.speciality_id
ORDER BY CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END,
speciality.name ASC
Its not good to use reserved keyword as a table or a column name..
group is a reserved keyword and that's why giving an error, you can use quotes tilt (`) to use it as a table name.
Also you can't use it as column name, see the related post:
group as a column name in insert command
I have the following query:
SELECT * FROM
(SELECT t1.`id`, t1.`vehicle`, lp1.`veh_no` AS `lp_vehicle`,
t1.`last_date`, t1.`due_date`, t1.`driver`, lp4.`employ_name` AS `lp_driver`
FROM `inspection` AS t1 LEFT OUTER JOIN `vehicle` AS lp1
ON (t1.`vehicle` = lp1.`id`)
LEFT OUTER JOIN `employee_driver` AS lp4
ON (t1.`driver` = lp4.`id`)) subq,
WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER by vehicle asc;
It processes through fine until I get to the WHERE clause.
This is what I get on the above:
#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
'WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER b'
at line 1
Can someone please point out what I am doing wrong? I'm running MySQL 5.1.48
you have an extra comma after subq