When I use count() function MySQL return one row. I need to use count function for pagination system, I don't have a better idea.
My SQL is:
SELECT *, count(id_question) AS num FROM `questions` LIMIT 0, 10
when you use any aggregate function, without the group by clause you see only one result.
The COUNT() is an aggregate function and so will group all the rows together. If you need the count of all rows just count the number of rows returned in whatever language you're using.
SQL does not allow to aggregate over the whole table and return all rows at the same time.
You have to use two queries, one for the total number of rows (count) and one for the rows themselves.
Have you tried ...
SELECT *, count(*) AS num FROM `questions` LIMIT 0, 10
or simply using mysql_num_rows() to get the count?
Since count is an aggregate function, it will by design only return one row. You would have two use two queries, one for the select and one for the count.
Alternatively you could do a nested select:
SELECT *, (SELECT count(id_question) from 'questions') AS num FROM `questions` LIMIT 0, 10
You can use mysql_num_rows($queryHandle) on your normal results query. An example:
$query = 'SELECT * FROM `questions` LIMIT 0, 10';
$queryHandle = mysql_query($query);
$count = mysql_num_rows($queryHandle);
$firstRow = mysql_fetch_assoc($queryHandle);
Try:
SELECT * FROM `questions` a
INNER JOIN (
SELECT count(`id_question`) as `num`
FROM `questions`
) b
LIMIT 0, 10;
Each row will now have the total row count of the table as a column called num.
Related
I have this single query.
$sql = "SELECT * FROM `my_table` WHERE type!='' AND (page_option='0' OR page_option='1') ORDER BY type ASC";
The first three results for the column scenario returned are a1,d3,e1.
Now when i do a UNION ALL with other identical tables:
$sql = "SELECT * FROM `my_table`
WHERE type!='' AND (page_option='0' OR page_option='1')
UNION ALL
SELECT * FROM `my_table_2`
WHERE type!='' AND (page_option='0' OR page_option='1')
UNION ALL
SELECT * FROM `my_table_3`
WHERE type!='' AND (page_option='0' OR page_option='1')
ORDER BY type ASC";
I loop through the results and the first three columns for my_table are e1,e1,e1.
Each table has a column called type, which identifies the table the result comes from. So for the union query results i added a condition to display only scenarios from my_table
if($type=="my_table") {
echo $scenario;
}
The scenarios should be identical to the single query but they aren't.
How do i solve?
Your assumption is wrong. Order Isnt guaranteed unless you use ORDER BY
In your case you are order only by type so the order of other fields can be random
You can find a nice explanation and examples here:
http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/05/20/without-order-by-there-is-no-default-sort-order.aspx
I'm going to count returned rows by this query:
select * from tableName where( number > 1000);
I tried this query to get rows count:
select count(*) as count from (select * from tableName where( number > 1000));
but I got syntax error. What is wrong with that?
You dont want Nested Query Just Use
select count(*) as count from tableName where number > 1000 ;
This works if you are using nested query & don't use 'count' as your temporary variable name:
select count(temp.id) as num from (select * from tableName where number > 1000) temp
It should be like this
select count(*) as noOfCount from tableName where number > 1000;
Do not use sql reserved keywords as your temp variable names
Why not counting the rows in 1 select directly:
select count(*) from tableName where number>1000;
Make it simple. Don't need a subquery
select count(*) as count from tableName where number > 1000;
Likely, the syntax error your are getting is "every derived table must have an alias".
To fix that syntax error, you would just assign an alias to the inline view query.
SELECT foo FROM (SELECT foo FROM bar) a ;
^
But for your specific query, an inline view isn't required.
You could simply modify your original query, to replace the * in the SELECT list with an aggregate expression such as COUNT(*). You can also assign an alias to the aggregate expression.
It's valid to use COUNT as a column alias, but my preference would be to use a different alias, one that isn't the name of a MySQL function.
FIrst thing first
SELECT COUNT(*) as tot_rows FROM `tableName` WHERE `number` > 1000 ;
you better give back tilt (`) sign arond number, cause might be there's a keyword in mysql called number. I am not pretty sure, but you should take precaution.
Second you can do another thing also.
If you are using PHP then
$query_result = mysql_query("SELECT * FROM `tableName` WHERE `number` > 1000");
$num_row = mysql_numrows($query_result);
This query works fine
select count(*) as count from tableName where number > 1000 ;
but FYR in ur query error is u don't define subquery name in example its tbl
select count(*) as count from (select * from tableName where( number > 1000)) as tbl;
I am writing following query to fetch all details of table bill_details.
select * from bill_details;
Along with data, I also want to fetch number of records in this table.
SELECT *, (SELECT COUNT(*) FROM bill_details) AS cnt
FROM bill_details
Every row of the results will have an additional column with the row count.
mysql_num_rows(mysql_query("select * from bill_details"))
You'll have to use two queries for that, or use mysqli_num_rows if you're using MySQLi, as suggested by Rakesh. If you're using PDO, you can use rowCount.
SELECT * FROM bill_details;
Followed by:
SELECT COUNT(*) FROM bill_details;
You can try the query below, but the first column will repeat the count information:
SELECT *
FROM (SELECT * FROM bill_details) A, (SELECT COUNT(*) FROM bill_details) C
I want to select all the rows from my table except the first 20 rows. How it possible? The total number of rows are not static.
SELECT statistics_id,title, user_name FROM (
SELECT statistics_id,title, user_name FROM statistics ORDER BY statistics_id DESC
LIMIT(select count(*)from statistics )-20
) sub
ORDER BY access_statistics_id ASC
I know 'LIMIT(select count(*)from statistics )-20' is not a correct method. Please help.
the documentation says (https://dev.mysql.com/doc/refman/5.5/en/select.html) the following:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
so you could use something like
LIMIT 20, veryLargeNumber
Try this
DECLARE v_max bigint unsigned default ~0;
SELECT statistics_id,title, user_name
FROM statistics
LIMIT 20, v_max;
After writing the select query u just have to include,
LIMIT 21,100;
21-Offset i.e from which row you want to start selecting and
100- is the Limit[Which you can set according to your need]
You actually need:
SELECT
statistics_id, title, user_name
FROM statistics
ORDER BY
statistics_id ASC
LIMIT 20, 18446744073709551615;
As per MySQL Documentation
To retrieve all rows from a certain offset up to the end of the result
set, you can use some large number for the second parameter. This
statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
If your table grows fast, selecting all rows (with exception of first 20) is not a good idea. In such case, you should batch your query, and handle a subset of entries at a time, some thing like:
SELECT * FROM tbl LIMIT 20,120;
Try using the offset option for the LIMIT syntax. you can read more about the LIMIT syntax at http://dev.mysql.com/doc/refman/5.0/en/select.html.
SELECT `statistics_id`
, `title`
, `user_name`
FROM `statistics`
ORDER BY `statistics_id` ASC
LIMIT 20, 18446744073709551615
I want to do a query like
select * from chr2;
but only have MySQL return the first tuple (or an arbitrary) tuple instead of all of them.
How do I do it?
Use the LIMIT clause:
SELECT * FROM chr2 LIMIT 1;
If you want an arbitrary row returned, you have to sort your rows by an random col like this (MySQL docu):
SELECT * FROM chr2
ORDER BY RAND()
LIMIT 1;
On large tables, however, you might run into performance problems with this, as there a random value has to be created for each row and the table has to be sorted according to this column.
Try this ::
select * from chr2 limit 1