I need to create a table from the union of two queries.
This query works exactly as I need it to:
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5
union
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3
Once I add the create statement I begin to get errors
CREATE TABLE portligh_lotteryTest.cTop8 (team int) AS
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5)
union
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3)
The error is:
#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 portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER ' at line 1
You can try below query:-
CREATE TABLE portligh_lotteryTest.cTop8 (team int) AS
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5
union all
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3)
Related
this is the code what im using
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5)
ORDER BY RAND()
LIMIT 1
');
This is the error what give me
Message: Error during SQL execution: SELECT VID, thumb FROM video WHERE VID IN ( SELECT VID FROM video WHERE title LIKE "%funny%" ORDER BY viewtime DESC LIMIT 5) ORDER BY RAND() LIMIT 1<br />
MySQL Error: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'<br />
MySQL Errno: 1235
how i can fix this problem ? its other way to make it ... so i dont get the error ...
Instead of using IN, you can use JOIN
SELECT v.VID, v.thumb
FROM video AS v
INNER JOIN
(SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) as v2
ON v.VID = v2.VID
ORDER BY RAND()
LIMIT 1
You can use below to bypass this error.
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (select * from (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) temp_tab)
ORDER BY RAND()
LIMIT 1
');
You don't need a subquery here. Try this:
SELECT VID, thumb
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY RAND() DESC
LIMIT 1
In MySQL 5.0.26 and later, you will get an error:
MySQL does not support LIMIT in subqueries for certain subquery operators:
Reference.
add this is your in condition
(SELECT * FROM (
SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)
Why you cant use simple: ?
SELECT v.VID, v.thumb
FROM video as v
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5
what for subqueries here?
mysql is disabled read it ORACLE
DELETE FROM wall_orders WHERE order_id IN (
SELECT order_id FROM (SELECT order_id, COUNT(orders_products_id) as cnt FROM wall_orders_products GROUP BY order_id ORDER BY cnt DESC LIMIT 1000) y1 WHERE cnt > 170 LIMIT 1000)
235 - This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Simple SQL and not possible
this is the code what im using
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5)
ORDER BY RAND()
LIMIT 1
');
This is the error what give me
Message: Error during SQL execution: SELECT VID, thumb FROM video WHERE VID IN ( SELECT VID FROM video WHERE title LIKE "%funny%" ORDER BY viewtime DESC LIMIT 5) ORDER BY RAND() LIMIT 1<br />
MySQL Error: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'<br />
MySQL Errno: 1235
how i can fix this problem ? its other way to make it ... so i dont get the error ...
Instead of using IN, you can use JOIN
SELECT v.VID, v.thumb
FROM video AS v
INNER JOIN
(SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) as v2
ON v.VID = v2.VID
ORDER BY RAND()
LIMIT 1
You can use below to bypass this error.
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (select * from (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) temp_tab)
ORDER BY RAND()
LIMIT 1
');
You don't need a subquery here. Try this:
SELECT VID, thumb
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY RAND() DESC
LIMIT 1
In MySQL 5.0.26 and later, you will get an error:
MySQL does not support LIMIT in subqueries for certain subquery operators:
Reference.
add this is your in condition
(SELECT * FROM (
SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)
Why you cant use simple: ?
SELECT v.VID, v.thumb
FROM video as v
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5
what for subqueries here?
mysql is disabled read it ORACLE
DELETE FROM wall_orders WHERE order_id IN (
SELECT order_id FROM (SELECT order_id, COUNT(orders_products_id) as cnt FROM wall_orders_products GROUP BY order_id ORDER BY cnt DESC LIMIT 1000) y1 WHERE cnt > 170 LIMIT 1000)
235 - This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Simple SQL and not possible
Trying my first sub query and cant seem to get the right syntax, tried many variations, not sure if i should use temp table or not. Im using this as a guide:
Source
#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 log.user_id = 16)AS tempTable' at line 11
SQL
UPDATE log
SET log.out_datetime = NOW()
WHERE log.log_id IN
(
SELECT log_id FROM
(
SELECT log.log_id
FROM log
ORDER BY log.log_id DESC
LIMIT 1
WHERE log.user_id = 16 //<- line 11
)
AS tempTable
)
Your statements are out of order. Also, you don't need IN, you can just use =.
More importantly, you don't need a subquery at all, because MySQL supports ORDER BY and LIMIT in UPDATE queries:
UPDATE log
SET log.out_datetime = NOW()
WHERE log.user_id = 16
ORDER BY log.log_id DESC
LIMIT 1;
It should be change as below.
Correct SQL syntax:
SELECT field-names FROM table-name WHERE field-name=value ORDER BY field-name DESC LIMIT number-of-result
UPDATE log
SET log.out_datetime = NOW()
WHERE log.log_id IN
(
SELECT log_id FROM
(
SELECT log.log_id
FROM log
WHERE log.user_id = 16
ORDER BY log.log_id DESC
LIMIT 1
)
AS tempTable
)
Try the code below. WHERE should come before the LIMIT or ORDER BY clause --
UPDATE log
SET log.out_datetime = NOW()
WHERE log.log_id IN
(
SELECT log.log_id
FROM log
WHERE log.user_id = 16
ORDER BY log.log_id DESC
LIMIT 1
)
i am trying to implement following query,
(SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
final_avg_total.`DRG Definition`,
final_avg_total.`Provider Id`,
SUM(final_avg_total.avg_total_payments) AS avg_payments,
SUM(final_avg_total.avg_covered_charges) AS avg_covered,
(SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_average,
1 - (SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_percentage
FROM final_avg_total
GROUP BY final_avg_total.`Provider Id`
ORDER BY total_average DESC LIMIT 0,5)
Union
SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
final_avg_total.`DRG Definition`,
final_avg_total.`Provider Id`,
SUM(final_avg_total.avg_total_payments) AS avg_payments,
SUM(final_avg_total.avg_covered_charges) AS avg_covered,
(SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_average,
1 - (SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_percentage
FROM final_avg_total
GROUP BY final_avg_total.`total_percentage`
ORDER BY total_average DESC LIMIT 0,5
actually both queries are almost same with only Group By is Differing, but i am getting this error.
5 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 'UNION (SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
fin'
It can be helpful to opt for a smaller problem, if possible.
See if you get the same results if you
CREATE OR REPLACE VIEW part_one AS ... ;
CREATE OR REPLACE VIEW part_two AS ... ;
and then
SELECT * FROM part_one
UNION
SELECT * FROM part_two;
Letting a database engine comprehend the work on it piece-wise has helped me in times past. Also can aid code maintenance.
If you get dupes, UNION ALL is another handy arrow in the quiver.
this is just a regular UNION with error
http://www.sqlfiddle.com/#!2/ec657/7
and this is the 'same' UNION without error
http://www.sqlfiddle.com/#!2/ec657/8
the only difference is the parenthesis in both case, please be sure to place parenthesis just after the UNION or remove it
...
ORDER BY total_average DESC LIMIT 0,5)
Union
(SELECT --ADDED PARENTHESIS
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
...
ORDER BY total_average DESC LIMIT 0,5) --ADDED PARENTHESIS
I've this stored procedure in which I am using a prepared statment to perform two queries.Here's the procedure.
SET #uid = puserid;
SET #rangee = plimit * 50;
SET #post = 'post';
PREPARE STMT FROM
'
SELECT notifications.postid,victims.victimid,friends.friendsname,notificationrecievers.status
FROM friends,victims,notifications,notificationrecievers
WHERE victims.postid=notifications.postid
AND notificationrecievers.notificationid=notifications.notid
AND notificationrecievers.recieverid=?
AND notifications.type=?
AND friends.friendsid=victims.victimid
AND notificationrecievers.notificationid <=
(
SELECT MAX(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)a
)
AND notificationrecievers.notificationid >=
(
SELECT MIN(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)b
)
ORDER BY notifications.postid DESC;
UPDATE notificationrecievers
SET notificationrecievers.status=1
WHERE notificationrecievers.status=0
AND notificationrecievers.recieverid=?
AND
notificationrecievers.notificationid <=
(
SELECT MAX(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)e
)
AND
notificationrecievers.notificationid >=
(
SELECT min(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)g
);
';
EXECUTE STMT USING #uid,#post,#uid,#rangee,#uid,#rangee,#uid,#uid,#rangee,#uid,#rangee;
When I run this query in the procedure it gives me syntax error.But when I use it directly by giving hard coded values in place of ? than it works fine.Why is this happening? puserid and plimit are the parameters which i pass to this procedure.
This is the 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 ';
UPDATE
notificationrecievers
SET notificationrecievers.st' at line 25