4

I am looking for MYSQL QUERY, not PLSQL QUERY

select *  from aadhar limit (select count(*)/2 from aadhar);

I tried to like this but getting this error please help

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 '(select count(*)/2 from aadhar )' at line 1

1
  • update your question and add the exact error message Commented Jan 29, 2019 at 13:42

6 Answers 6

4

Like the other answers already explained LIMIT can't be dynamically set in a query.
But you can workaround it with a MySQL user variable in combination with PREPARE/EXECUTE..

SET @count = (SELECT COUNT(*)/2 FROM aadhar); 
SET @sql = CONCAT('SELECT * FROM aadhar LIMIT ', @count);

PREPARE q FROM @sql;
EXECUTE q;
DEALLOCATE PREPARE q;

Or (safest option)

SET @count = (SELECT COUNT(*)/2 FROM aadhar); 

PREPARE q FROM 'SELECT * FROM aadhar LIMIT ?';
EXECUTE q USING @count;
DEALLOCATE PREPARE q;

Extra warning

Please note that SQL is by definition orderless. Using LIMIT without ORDER BY is meaningless. SQL is free to return the records what matches in anny order it wishes without ORDER BY meaning running the same query twice might result in a different result.

Also it's better to use CEIL() or CAST() which converts a double into a int so the methodes will not error when the table has a odd number off records and generates SQL with a double like this SELECT * FROM aadhar LIMIT 1.500000000

see demo on how to do those

Sign up to request clarification or add additional context in comments.

Comments

3

Not perhaps what you would like but- 'Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.' https://dev.mysql.com/doc/refman/5.5/en/select.html

IF you don't want to use dynamic sql or a stored procedure but you do have some way of ordering your table then row number simulation (pre version 8) or row_number window function (version 8 on) may help. Given

+-----+----------+
| id  | username |
+-----+----------+
|   1 | aaa      |
|   2 | Jane     |
|   3 | Ali      |
|   6 | Bruce    |
|   7 | Martha   |
|   8 | Sidney   |
|  10 | charlie  |
|  12 | Elisa    |
|  14 | Samantha |
|  15 | Hannah   |
|  16 | Hannah   |
|  17 | Kevin    |
| 999 | bob      |
+-----+----------+
13 rows in set (0.00 sec)

SELECT S.ID,S.USERNAME FROM
(
SELECT U.ID,U.USERNAME,
         @RN:=@RN+1 ROWNUMBER,
         (SELECT ROUND(COUNT(*)/2) FROM USERS) TOPN
FROM USERS U
CROSS JOIN (SELECT @RN:=0) R
ORDER BY U.ID 
) S
WHERE S.ROWNUMBER <= S.TOPN;

will result in

+----+----------+
| ID | USERNAME |
+----+----------+
|  1 | aaa      |
|  2 | Jane     |
|  3 | Ali      |
|  6 | Bruce    |
|  7 | Martha   |
|  8 | Sidney   |
| 10 | charlie  |
+----+----------+
7 rows in set (0.00 sec)

3 Comments

this is not for Within stored programs . i just want to select first 50% records from a table as i know query in PLSQL is select rownum, e.* from aadhar a where rownum<=(select count(*)/2 from aadhar);
MYSQL is not plsql - there is no equivalent of rownum in mysql.
Hi P.Salmon . i know PLSQL and MYSQL are different. here i am to know equivalent query to get first 50% records from a table
2

There is no provision in MySQL to select and show half of the total records in the table until unless you explicitly specify the scalar in LIMIT clause.

Comments

0

You can achieve this using variables.

SELECT  @s:=@s+1 id,a.*
FROM    aadhar a,
        (SELECT @s:= 0) AS s where @s < (SELECT count(*)/2 from aadhar a2)

Here '@s' is a variable and adding create id which is autoincremented. Where condition store half of your total records count and @s stop just after reaching the limit of half count of your record.

Comments

-1

Try this:

select *  from aadhar limit ((select count(*) from aadhar)/2);

The count clause returns the number of records, and half of that value is taken in limit.

Comments

-1

select * from (select ,row_number() over() as rowno from aadhar) a where a.rowno <= (select ceil(count()/2) from aadhar) ;

1 Comment

Posting just code as the answer is not an answer, and it requires explanation as to what the code does, and why this answer is different than the other 5 existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.