sql error with inner join mysql - mysql

String sql = "set #row \\:=-1; SELECT ue.id, ue.Latitude, ue.Longitude, ue.Serving_Cell, ue.RSCP
FROM ue INNER JOIN(
SELECT id
from (
SELECT #row \\:=#row+1 as rownum, id
from (
select id from ue order by id) as sorted
)as ranked
where rownum %20=0) as subset
on subset.id = ue.id
where Operator like :operator ";
Query query = getSessionFactory().getCurrentSession().createSQLQuery(sql);
query.setParameter("operator", "%" + operator + "%");
return query.list();
help me please to find my sql error
console render me :
GRAVE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/SpringSecurity] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ue.id, ue.Latitude, ue.Longitude, ue.Serving_Cell, ue.RSCP FROM ue INNER ' at line 1

You try to execute 2 queries at once. You can only execute one at a time.
SELECT ue.id, ue.Latitude, ue.Longitude, ue.Serving_Cell, ue.RSCP
FROM ue INNER JOIN(
SELECT id
from (
SELECT #row \\:=#row+1 as rownum, id
from (
select id, #row
from ue, (select #row := -1) r
order by id) as sorted
)as ranked
where rownum %20=0) as subset
on subset.id = ue.id
where Operator like :operator
But you can use a subquery to initialize a variable like this
(select #row := -1) r

Related

How to use VIEW in WHERE clause (MySQL)?

I want to use data of view in WHERE clause. But getting an error:
create view post_with_answers AS
SELECT DISTINCT postid
FROM (SELECT postid FROM `qa_posts` WHERE `type` = 'Q') AS q1
INNER JOIN (SELECT parentid FROM `qa_posts` WHERE `type` = 'A') AS q2 ON q1.postid = q2.parentid
select count(*)
from qa_posts
where parentid not in post_with_answers
On the last row I am getting this error:
SQL 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 'post_with_answers' at line 3
How to fix that?
Just like you would use a table:
select count(*)
from qa_posts
where parentid not in (select pwa.postid from post_with_answers pwa);
I would caution you from using not in with a subquery. No rows are returned if even one value from the subquery is NULL. For this reason, I recommend NOT EXISTS:
select count(*)
from qa_posts p
where not exists (select 1
from post_with_answers pwa
where p.parentid = pwa.postid
);
In addition, your view definition is a bit over complicated. You don't need subqueries:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq JOIN
qa_posts pa
ON pq.postid = pa.parentid
WHERE pq.type = 'Q' AND pa.type = 'A';
Then, the DISTINCT just adds overhead, so EXISTS is better:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq
WHERE EXISTS (SELECT 1
FROM qa_posts pa
WHERE pq.postid = pa.parentid AND
pa.type = 'A'
)
WHERE pq.type = 'Q';

Column not found in Select statement inside select statement

Trying to select one row at a time from this query (for example where the rank = 1). However, it doesn't work because "unknown column 'rank'. if i change it to "WHERE id = 1" then it works, but for some reason it doesn't know what rank is even tho it is set
SET #rownum=0;
SELECT #rownum := #rownum + 1 AS rank, id, client, date, time, pickupCity, pickupState
FROM (
SELECT r.id, CONCAT(c.fname, ' ', c.lname) as client, r.date,
LOWER(TIME_FORMAT(r.time, '%l:%i%p')) as time, r.pickupCity, r.pickupState
FROM request r
INNER JOIN client c ON r.client = c.id
INNER JOIN pickup p ON r.id = p.request
INNER JOIN driver d ON d.id = p.driver
WHERE date = '2018-04-18' AND d.id = 1
GROUP BY time
) AS tab
HAVING rank = 1;
In MySQL, you can do this using HAVING:
SELECT #rownum := #rownum + 1 AS rank, id, client, date, time, pickupCity, pickupState
FROM (SELECT r.id, CONCAT(c.fname, ' ', c.lname) as client, r.date,
LOWER(TIME_FORMAT(r.time, '%l:%i%p')) as time, r.pickupCity,
r.pickupState
FROM request r JOIN
client c
ON r.client = c.id JOIN
driver d
ON ?
pickup p
ON d.id = p.driver
WHERE date = '2018-04-18' AND d.id = 1
GROUP BY time
) t CROSS JOIN
(SELECT #rank := 0) params
HAVING rank = 1;
Notes:
The ?? is for the missing JOIN conditions.
I went through the effort of fixing your JOIN syntax. You should go through the effort of learning proper, explicit, standard JOIN syntax.
You can set #rank in the query; you don't need a separate statement.
The GROUP BY makes no sense, because you have many unaggregated columns in the SELECT.
If I had to speculate, the root cause of your problems is the missing JOIN condition, and you've put a lot of effort into working around that.

Multiple JOINs too complex

This has become more complex than I can follow. I have modified this query from another post here and now I need to add another join, but it's not working.
Here is the current query in MySQL:
SELECT * FROM (SELECT *,
#rn := IF(#prev = class, #rn + 1, 1) AS rn,
#prev := class
FROM HeatWaveData
JOIN
(SELECT #prev := NULL, #rn := 0) AS vars
ORDER BY class, score DESC)
AS T1
WHERE rn <= 3
AND score > 0
AND dsqreason = ''
AND class <> ''
Now I need to JOIN (SELECT * FROM Awards WHERE active ON Awards.award = HeatWaveData.award)
but it fails with errors. I've tried several changes and different things but keep getting errors.
Here is my updated query and error:
SELECT * FROM (SELECT *,
#rn := IF(#prev = class, #rn + 1, 1) AS rn,
#prev := class
FROM HeatWaveData
JOIN
(SELECT #prev := NULL, #rn := 0) AS vars
ORDER BY class, score DESC)
AS T1
LEFT JOIN
(SELECT * FROM Awards WHERE active = '1'
ON Awards.award = T1.award) AS T2
WHERE rn <= 3
AND score > 0
AND dsqreason = ''
AND class <> ''
Error:
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
'ON Awards.award = T1.award) AS T2 WHERE rn <= 3 AN' at line 11
Just try to replace your sub-select with a "normal" join:
old:
LEFT JOIN
(SELECT * FROM Awards WHERE active = '1'
ON Awards.award = T1.award) AS T2
new:
LEFT JOIN Awards AS T2
ON T2.award = T1.award AND T2.active = '1'

how vb.net by pass or ignored contains # in sql query?

I Have a Query in mysql like this:
select
t1.id_invoice_in, t1.paymentcode, t1.personname, t1.amount,
t2.id_invoice_out, t2.paymentcode, t2.personname, t2.amount
from (
select
id_invoice_in, paymentcode, personname, amount,
(#r1 := #r1 + 1) as r1
from invoice_in
join paymenttype pt on pt.id = paymenttype_id
join persons p on p.id = person_id
,(select #r1 := 0) r
) t1 left join (
select
id_invoice_out, paymentcode, personname, amount,
(#r2 := #r2 + 1) as r2
from invoice_out
join paymenttype pt on pt.id = paymenttype_id
join persons p on p.id = person_id
,(select #r2 := 0) r
) t2 on t1.r1 = t2.r2;
Sample SQL Fiddle
That query working fine on phpmyadmin. When i put the query in VB.net like this:
myCMD = New MySqlCommand(strQuery, MysqlConn)
mysqlReader = myCMD.ExecuteReader()
i got the error: "fatal error encountered during command execution" from exception message.
i guess maybe the query containt this symbol '#', so i must write myCMD.Parameters.AddWithValue("#", something else). *correct me if im wrong.
the question is, how vb.net by pass the query without entry myCMD.Parameters.AddWithValue. So vb.net just running the query without must entry '#' value, because no value for '#'.

Counting rows from different tables in same query

I thought i did something like this before:
$reg = mysql_query ("(SELECT count(*) from vouchers) as total_vouchers),
(SELECT count(*) from vouchers WHERE asignado = 1) as vouchers_asignados,
(SELECT count(*) from crm) as crm_users,
(SELECT count(*) from datos_modificados) as dm_users") or die(mysql_error());
But it would return mysql_error near the first , :
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 total_vouchers), ((SELECT count(*) from vouchers WHERE asignado = 1) a' at line 1
Any idea why?
Try to add select as below
$reg = mysql_query ("SELECT
(SELECT count(*) from vouchers) as total_vouchers,
(SELECT count(*) from vouchers WHERE asignado = 1) as vouchers_asignados,
(SELECT count(*) from crm) as crm_users,
(SELECT count(*) from datos_modificados) as dm_users") or die(mysql_error());