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;
Related
I have tried to run a select query in phpmyadmin Sql tab as below
Select * from followers
I just run above query alone
But it return following Error in the image, can anybody help to fix this issue? can you tell me in what case it will happends?
You should use comma for adding others column in order by
Select * from followers
order by follwer_id DESC, leader_id
limit 30
or if you want filter the query you should use where
Select * from followers
where leader_id =1
order by follwer_id
limit 30
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 have the following query:
SELECT owner,
typeOwner,
type,
count(*) AS count
FROM myTable
GROUP BY typeOwner
ORDER BY count DESC
LIMIT 0, 30
Now , the typeOwner values = '<test>a</test>' but , some times the typeOwner field will have some string else like '<test>b</test>, how can i let this query count the <test>b</test> as a group of '<test>a</test>.
I want to make an exception for this, I mean typeOwner <test>a</test> AND typeOwner <test>b</test> should be counted as one row that have two count.
here's a fiddle : http://sqlfiddle.com/#!2/70053/1 , take look
actually these are should be grouped by <type></type><aId></aId>
"actually these are should be grouped by "
You can GROUP BY something relatively ugly like:
GROUP BY LEFT(typeOwner, position('<xType>' in typeOwner) -1)
But you are probably better off preprocessing the data in some fashion. I'm not sure how MySQL handles xml, but in SQL server I might extract the XML values into first class relation fields if I needed to do this sort of processing with any frequency.
sqlfiddle.com
Based on your sqlfiddle, this works
SELECT
left(typeOwner, instr(typeOwner, '<xType>')-1) as typeOwner,
owner,type, count(*) as count
FROM `test`
GROUP BY left(typeOwner, instr(typeOwner, '<xType>')-1)
ORDER BY count DESC
LIMIT 0 , 30
I am running the follwoing SQL query:
SELECT * FROM COUNTRIES WHERE USERTYPE = 'PREMIUM' ORDER BY SALES DESC;
The output of this query yields a list of countries.
Now I need to populate a field in another table which is like TOP_SALES_COUNTRY, SECOND_TOP_SALES_COUNTRY and THRID_TOP_SALES_COUNTRY for which I only need the first,second and third records in the output of this SELECT statement. Kindly advise on how this can be achieved.
SELECT * FROM COUNTRIES WHERE USERTYPE = 'PREMIUM' ORDER BY SALES DESC limit 0,1; to get the first row
What worked for me is a modification of reverse_engineer's answer. FOr example to get 2nd top most country i used LIMIT 1,1 and for third value I used LIMIT 2,1. Thank you for your help. Immensely grateful to all.
You could use any of the following:
Option 1:
SELECT TOP number|percent column_name(s)
FROM table_name;
Option 2:
SELECT column_name(s)
FROM table_name
LIMIT number;
Option 3:
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
I am using phpMyAdmin to write some SQL code that I thought was simple but proving to be a headache. I'm using this tutorial to help me out. My goal is to get the first and last columns id's from a result set. When I do this query I get 5 rows starting at 15 and going through 11.
SELECT id
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
However, when I try this query I get an error #1064: "You have an error in your SQL syntax."
SELECT FIRST(id)
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
Something like this maybe?
SELECT min(id), max(id)
from (
select id
from boardPost
where recipientId = 1
order by id desc
limit 0,5
) t
I think that is what you want?
Select id from boardPost order by id asc limit 1
and
Select id from boardPost order by id desc limit 1
If you just want the first and the last id of a result set, you could consider this:
SELECT MIN(id) firstId, MAX(id) lastId FROM someTable WHERE aField = 1;
Note that it'll only work if you do use and ORDER BY an AUTO_INCREMENT field, else you might get unexpected results.
It'll only work with the full set. If you need the first and last id of a limited one, you're probably better of using 2 queries with ASC and DESC order and LIMIT 1.
MySQL does not support the FIRST() function. You will need to use the workaround they specified in that tutorial (using ORDER BY and LIMIT)
In some situations (like mine, that first brought me here), there are some rarely-used MySQL "windowing functions" such as FIRST_VALUE and LAST_VALUE that may provide the functionality you're seeking.
(Here's more info on Window Function Concepts and Syntax, and the Wikipedia description of the term.)