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;
Related
SELECT *
FROM TABLE_X
WHERE 'Value' IN (SELECT * FROM TABLE_Y)
What expression can I use instead of 'Value' so that the WHERE clause where always return true?
I am trying to do something like:
WHERE (SELECT field FROM TABLE_Y) LIKE '%Value%'
Found the solution, use EXISTS instead of WHERE IN:
SELECT *
FROM TABLE_X
WHERE EXISTS (SELECT field FROM TABLE_Y WHERE field LIKE '%Value%')
I don't know if I understood it well, it's silly to do the following, but returns all the records in TABLE_X:
SELECT *
FROM TABLE_X
WHERE 1 IN (SELECT field FROM TABLE_Y)
NOTE: ...IN (SELECT * ...) crashes because you should choose just one column, that's why I wrote field instead of *
You cannot compare your 'Value' with *, you need a specified field from TABLE_Y, for example:
SELECT *
FROM TABLE_X
WHERE (SELECT Field FROM TABLE_Y WHERE Field LIKE '%Value%' LIMIT 1) IS NOT NULL
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'm trying to understand COUNT(*), and therefore I created a testing query:
SELECT COUNT(*)
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
When I click Execute, I get the following error:
Syntax error (operator missing) in query-expression COUNT(*) WHERE COUNT(UITLENINGEN.LLNR) >= 30.
What am I doing wrong?
Try this
SELECT COUNT(*) FROM UITLENINGEN GROUP BY LLNR HAVING COUNT(UITLENINGEN.LLNR) >= 30;
I don't understand what you're trying to do. The query below is based on a table which includes a field named category_id. And it uses GROUP BY category_id to count the number of rows within each such group. The HAVING clause limits the result set to only those groups whose count is at least 30.
SELECT category_id, COUNT(*)
FROM YourTable
GROUP BY category_id
HAVING COUNT(*) >= 30;
If that is nothing like what you're trying to accomplish, please give us more detailed information so we may better understand your situation. A brief set of sample data, and the output you want based on that sample, would help tremendously.
You have not specified the table from which the data should be retrieved. Try the following
SELECT COUNT(*) from tableName
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Add your table name to the query.
SELECT COUNT(*) FROM UITLENINGEN WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Please, add table name and use having statement where aggregation funcion required. E.g.:
select count(*)
from UITLENINGEN
having count(UITLENINGEN.LLNR) >= 30;
I am trying to count line that has columns which been repeated (their value) more than x times in mysql, every time I try to execute this query I get an error :
SELECT DISTINCT
idLogin, count(idLogin ) AS "cou"
FROM
`products` AS t
GROUP BY
idLogin
HAVING
t.cou > 2
why this error is happening
Use this one
SELECT DISTINCT idLogin, count(idLogin ) AS cou FROM `products`
GROUP BY idLogin HAVING cou > 2
you can put that expression count(idLogin ) directly in having clouse....
it will not give any error.. and will be more understable as well....
or else you can do one thing -
select idLogin,cou from (
SELECT DISTINCT idLogin, count(idLogin ) AS cou
FROM products
GROUP BY idLogin ) t
where t.cou >2
Remove the double quotes from the aliased column name.
MySQL uses single back-quotes for escaping table and column names, not double quotes.
The correlation name t does not have a column cou. Just use this:
HAVING cou > 2
PS: DISTINCT is redundant in your query. The GROUP BY ensures there will only be one row per distinct value of idLogin.
I need to find all rows in my table where the strings of a specific field are duplicates in two or more places.
Can that be done in a MySQL statment?
EDIT
I need to get every row not just a count of how many duplicates there are. I want to be able to edit the fields.
Yes, try something like this:
SELECT *
FROM `YourTable`
WHERE `YourColumn` IN (
SELECT `YourColumn`
FROM `YourTable`
GROUP BY `YourColumn`
HAVING COUNT(*) > 1
)
Yes, using GROUP BY and HAVING.
SELECT mycolumn, count(*) FROM mytable
group by mycolumn
having count(*) > 1