Condition as a column named using AS "name" in mysql - mysql

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.

Related

Avoiding redundant expressions in SELECT query

Is there any way to avoid repeating column expressions in the SELECT query? I want to divide the sum and count of a column but would like to use the assigned name instead of repeating SUM(value)/COUNT(value) or using a sub query. Is this possible? If so, does that speed up the query by not repeating the calculation of the sum and count or does mysql remember already calculated expressions?
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, type_sum/type_count
FROM (...) AS datalist
GROUP BY datalist.type
throws: #1054 - Unknown column 'type_sum' in field list
Unless you put it in outer query, this is the only way.
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, SUM(value)/COUNT(value)
FROM (...) AS datalist
GROUP BY datalist.type
One workaround would be to use a alias table with pre-defined calculations and then later call it from outer table such as:
select d.type_sum/d.type_count as dividedValue from (SELECT datalist.type, SUM(value)
AS type_sum, COUNT(value) AS type_count
FROM (...) )AS d
GROUP BY d.type

Mysql: Is it possible to use a subquery inside a from clause in order to pick the table name from another table

I was wondering if there's any way to add a subquery with a switch case to the form clause of my select query in order to select a table based on a condition.
For example:
select a.*
from (select (case when (table2.column = 'something')
then (table2.tablename1)
else (table2.tablename2)) as tablename
from table2
where table2.column2 = 'blabla'
limit 1
) a
I tried to write that in many variation & so far non of them worked.
On the most successful tryouts (when I got no mysql errors) it returned the name of the table as the result itself (for example: the value that's in table2.tablename2). I understand why it did that (because I selected everything from a select results...) but how can I use the tablename from the results in order to set the table on the main query?
Hope that make sense...
Any idea?

Concat 2 columns in a string, then get a count for each concatenation

I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC

What is this SQL query syntax mean?

SELECT b.*,
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1
From this sql syntax, what is "," which after "*" mean?
and anyone can explain this query to me or tell me where I can start?
It means all the columns from table Booking, and to the far right (the last column per row) bring in the user_name column from table user relating on the user.user_id matching the booking.booking_inspector. Such that the Booking.confirm is 1, and Booking_inspector is filled in with a parameter passed.
So it limits the rows of output to confirm is 1 and Booking_Inspector is the parameter passed (or bound, etc) depending on the language calling it.
Select * means all columns. So all columns from the one table, and just one column from the other
In this case, (select a.USER_NAME from A.db.USER a where a.USER_ID=b.Booking_Inspector) is the subquery which will return column a.USER_NAME. So this query is selecting everything from b (b.*) and the column a.USER_NAME from the subquery. So like you put comma between column names in select query, it is same.
select all columns from b and one more column from that subquery as USER_NAME.
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
That whole thing above as 1 column
SELECT b.*, [USER_NAME]
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1

counting rows of selected data in mysql

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;