How to check if limit was used in mysql? [duplicate] - php

This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 7 years ago.
In mysql query, I have something like I select and order, and use where clause, etc..., and finally I have limit 10 for example. Something like this:
Select *
From employee
where fired=0
limit 10
The problem is, how do I know if limit was used and the result was truncated? Basically I want to know easily if there were more results that got cut off from limit. I don't want to do something like
Select (count *)
From employee
where fired=0
subtract
Select (count *)
From employee
where fired=0
limit 10
Is there a simple way for this? Perhaps, like you know how after you run an insert statement, you can get the id using php built in function get_sql_id() (something like that), is there a sql_count_without_limit()?
Thanks

You could do
Select *
From employee
where fired=0
limit 10+1
So that:
0-10 values returned: no limit was reached
11 values returned: Limit was reached
And then only use the first 10 values that are returned.
update Raymond's answer is waaaaay better than mine!

You can use the found_rows function like this:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
For more information see mysql reference

You can return the remaining rows as an column like below
SET #limit = 10;
SELECT *,
(SELECT count(*) from employee)- #limit as remaining
From employee
where fired=0
limit 10

Related

Selecting most recent row from a table [duplicate]

This question already has answers here:
Select last row in MySQL
(11 answers)
Closed last year.
id
first_die
24
2
25
6
How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.
Use:
select id,first_die
from test_tbl
order by id desc limit 1 ;
Demo:
Or:
select id,first_die
from test_tbl
where id = (select max(id) from test_tbl);
Demo:
If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).
If you want to return the entire recordset or row use the code below
SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);

Show Orderby Array value Value in SQL Query When using IN() [duplicate]

I have a PHP array with numbers of ID's in it. These numbers are already ordered.
Now i would like to get my result via the IN() method, to get all of the ID's.
However, these ID's should be ordered like in the IN method.
For example:
IN(4,7,3,8,9)
Should give a result like:
4 - Article 4
7 - Article 7
3 - Article 3
8 - Article 8
9 - Article 9
Any suggestions? Maybe there is a function to do this?
Thanks!
I think you may be looking for function FIELD -- while normally thought of as a string function, it works fine for numbers, too!
ORDER BY FIELD(field_name, 3,2,5,7,8,1)
You could use FIELD():
ORDER BY FIELD(id, 3,2,5,7,8,1)
Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.
It's kind of an ugly hack though, so really only use it if you have no other choice. Sorting the output in your app may be better.
Standard SQL does not provide a way to do this (MySQL may, but I prefer solutions that are vendor-neutral so I can switch DBMS' at any time).
This is something you should do in post-processing after the result set is returned. SQL can only return them in an order specified in the "order by" clause (or in any order if there's no such clause).
The other possibility (though I don't like it, I'm honor-bound to give you the choice) is to make multiple trips to the database, one for each ID, and process them as they come in:
select * from tbl where article_id = 4;
// Process those.
select * from tbl where article_id = 7;
// Process those.
: : : : :
select * from tbl where article_id = 9;
// Process those.
You'll just need to give the correct order by statement.
SELECT ID FROM myTable WHERE ID IN(1,2,3,4) ORDER BY ID
Why would you want to get your data ordered unordered like in your example?
If you don't mind concatening long queries, try that way:
SELECT ID FROM myTable WHERE ID=1
UNION
SELECT ID FROM myTable WHERE ID=3
UNION
SELECT ID FROM myTable WHERE ID=2

Get N value from database mysql

i have a table i get cars form databases and i list it in this table:
$row_id=$_GET["id"];
$solK = ($row_id-1) * 9;
$sagK = ($row_id) * 9;
$sorgu2 = mysql_query("SELECT * FROM Car WHERE Car_ID > '$solK' AND Car_ID < '$sagK'");
Every page have 9 cars i use id for sort these cars but when i delete a car (for example Carid=5) in first page have 8 cars but other pages have 9 cars how can i get first N values without CarId from databases can you explain with sql codes.
Add a LIMIT to your query.
For example
SELECT * FROM tbl LIMIT 0, 9
will select the first 9 entries from tbl.
In order to match your query and preserve the ordering I'd state it as
SELECT * FROM Car ORDER BY Car_ID LIMIT 0, 9
for the first nine rows. For the next nine rows, just increment both numbers by 10 and so on.
Rather than code it like you have done, just use LIMIT:
SELECT * FROM Car LIMIT 0,9
then
SELECT * FROM Car LIMIT 9,9
Use "LIMIT offset, row_count" in you statement
Try this SQL statement SELECT * FROM Car LIMIT '$pageN*$nperpage', '$nperpage'
Where $nperpage will be a number of items per page and $pageN will be a page number (note that in this case page numbering starts with 0).
http://dev.mysql.com/doc/refman/5.0/en/select.html
I assume the other answers (using limit and offset) will be suited for your case. But if you (or anyone else) ever need to improve performance, and need to manage fast queries for more than the first few pages, you should implement paging like this:
SELECT f1, f2, ...
FROM tbl
WHERE Car_ID > $id
ORDER BY Car_ID
LIMIT 10

Get number of rows in MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest way to count exact number of rows in a very large table?
What query do I use to get the number of rows that share a common specification.
Example: The number of rows where idpost = 3
You can use COUNT() documented here.
SELECT COUNT(1) FROM posts WHERE idpost = 3
EDIT: Updated according to dbf's suggestion.. Make sure you distinguish between COUNT(*) and COUNT(1), discussed here.
The query looks like this:
SELECT count(*) FROM some_table WHERE (conditions);
In your example:
SELECT count(*) FROM some_table WHERE idpost=3;
More on counting rows in MySQL: MySQL 5.6 Reference Manual: 3.3.4.8. Counting Rows
EDIT:
If you were wondering, which way of counting all rows is better (count(*) or count(1)), see this: What is better in MYSQL count(*) or count(1)?.
Try
SELECT
COUNT(*) as NumberRows
FROM
your_table_name_here
WHERE
idpost = 3
;
Consider learning SQL:
select count(*) from mytable where idpost=3
the select count(*) function grabs the number of rows that meet a certain criteria - define that in the where statement.
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html - read here

How to draw a name in a database [duplicate]

This question already has answers here:
How to request a random row in SQL?
(30 answers)
Closed 8 years ago.
I have names in my database and I want to draw a name for a contest.
Anyone have an idea for that ?
Thanks !!
SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1
This works in constant time, regardless of the table size, if num_value is indexed. One caveat: this assumes that num_value is equally distributed in the range 0..MAX(num_value). If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).
A query like this could work
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

Categories