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
Related
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
Please have a look at the below query.
SELECT sub_words.idwords, words_inc.idArticle
(
SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS excl_words, COUNT(sub1.idwords) AS older_words_cnt
FROM words_learned sub0
LEFT OUTER JOIN words_learned sub1
ON sub0.userId = sub1.userId
AND sub0.order < sub1.order
WHERE sub0.userId = 1
GROUP BY sub0.idwords
) sub_words
INNER JOIN words words_inc
ON sub_words.idwords = words_inc.idwords
LEFT OUTER JOIN words words_exc
ON words_inc.idArticle = words_exc.idArticle
AND FIND_IN_SET(words_exc.idwords, sub_words.excl_words)
WHERE words_exc.idwords IS NULL
ORDER BY older_words_cnt
LIMIT 100
This gives the 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 'SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS exc' at line 3
I checked the sub query individually and there was no error in the sub query! what is going on here?
You missed the from keyword
SELECT sub_words.idwords, words_inc.idArticle
FROM
( ...
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
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