I have this code:
#people.count
Which produces a query beginning with:
SELECT COUNT(*) FROM ( SELECT * .....
And this error:
column "peoplerelationships.updated_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 5: ORDER BY peoplerelationships.updat...
If I query just #people, there is no error. By requesting a COUNT it introduces an aggregate function to the query and hence the error. Can you ensure #people is evaluated first, and then counted? How do you do this properly?
Related
I'm trying to get the maximum number to "max"
getting error:
Error Code: 1111. Invalid use of group function 0.000 sec
SELECT max(count(*)) as max
FROM ticket
group by fan_fan_id;
I'm not sure what is the problem here and I will be glad to get some help here - also I need to solve it without "limit 1" option
SQL does not allow nesting aggregate functions like the example you show.
The argument to an aggregate function must be a scalar expression, not an aggregate expression.
You can do what you want this way:
SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ticket GROUP BY fan_fan_id) AS t;
Or an alternative is to sort by value descending, and return only the first count:
SELECT COUNT(*) AS c FROM ticket GROUP BY fan_fan_id ORDER BY c DESC LIMIT 1;
I written the query without ISNULL Function in mysql
SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice,f.ltp,f.prevclprice AS CLOSE,(((LTP-prevclprice)/(prevclprice*100))) AS per_change,f.expdate
FROM stmp_stocks_qty AS s
JOIN tbl_intraday_nsefoprice_latest AS f ON s.st_symbol = f.symbol AND f.ID IN (SELECT MAX(ID) FROM tbl_intraday_nsefoprice_latest GROUP BY SYMBOL)
ORDER BY s.st_symbol,f.ExpDate DESC,f.createdon DESC;
This query is working fine and while I execute this query I get the null values in my column based on the calculation "(((LTP-prevclprice)/(prevclprice*100)))" .
ex:
To avoid this null value I used ISNULL Function and written a query like
SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice AS CLOSE,ISNULL(((LTP-prevclprice)/(prevclprice*100)),0) AS per_change,f.expdate
FROM stmp_stocks_qty AS s
JOIN tbl_intraday_nsefoprice_latest AS f ON s.st_symbol = f.symbol AND f.ID IN (SELECT MAX(ID) FROM tbl_intraday_nsefoprice_latest GROUP BY SYMBOL)
ORDER BY s.st_symbol,f.ExpDate DESC,f.createdon DESC;
But while I executing this query I'm getting this king of error
Query: SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice AS CLOSE,isnull(((LTP-prevclprice)/(prevclprice*100)),0) AS per_change,f.expd...
Error Code: 1582
Incorrect parameter count in the call to native function 'isnull'
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
---------------------------------------------------
Here my intension is if any column is passing null value into the column it should became a 0.
Suggest me how can I achieve this without error.
I'm new to mysql
(Sorry for my bad english)
The MySQL equivalent of ISNULL is IFNULL
or
you can use coalesce(), so your query will be:
SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice AS CLOSE, coalesce(((LTP-prevclprice)/(prevclprice*100)),0) AS per_change,f.expd...
ISNULL() is a function which accepts only one argument. Function tests does the argument IS NULL and returns TRUE (1) or FALSE (1) depends on the testing result.
The function which accepts 2 arguments and returns first value if it is not NULL and second value otherwise is IFNULL().
I was attempting an error-based sql injection when I discovered a weird behavior that I can't explain. Here is a simple example:
SELECT * FROM users WHERE username = '' union SELECT extractvalue(rand(),concat(0x3a,(SELECT 1)));
Output: ERROR 1105 (HY000): XPATH syntax error: ':1'
But...
SELECT * FROM users WHERE username = '' union SELECT extractvalue(rand(),concat(0x3a,(SELECT 1 FROM users)));
Output: ERROR 1222 (21000): The used SELECT statements have a different number of columns
Now the second result is expected, because my users table have 3 columns. The first example is the one I don't understand.
Tested on 5.7.23-0ubuntu0.16.04.1
From what I could tell from messing around with it, because you are using a subquery, the expression is being evaluated in a different order. If you do this:
SELECT some_col FROM users WHERE username = '' union SELECT extractvalue(rand(),concat(0x3a,(SELECT 1 FROM users LIMIT 1)));
Essentially resolving the "different number of columns" exception, I think you'll get the same error as the first line again. At least in my testing, that's what happened. I guess it has to do with when the subquery is evaluated, cause that has to happen before the extractvalue() call can be completed.
I'm also pretty sure mysql doesn't read "SELECT 1" as a subquery, but rather probably just discards the "SELECT" entirely.
I got an error message
subquery returns more than one value
SELECT title,booking_date
FROM services,user_booking
where services.id=(
select service_id from user_booking where user_id in
(
select distinct id from users where users.email='test')
)
If you use a = condition, you have to make sure that your subquery only returns 1 row. Otherwise, use the in condition.
I want to run two mysql SELECT statements, combine them, call the new combination by its own name, then order that new combination by a user-defined function. This is what I am trying currently:
SELECT * FROM (
SELECT * FROM dictionary WHERE def1 LIKE '$input%'
UNION ALL
SELECT * FROM dictionary WHERE def2 LIKE '$input%'
) AS newcol
ORDER BY levenshtein('$input', newcol)
LIMIT 10
But I get the following error:
Unable to run query:Unknown column 'newcol' in 'order clause'
The problem is clearly with defining the new group 'newcol'.
You're trying to order by a TABLE, not by a FIELD! Use a field from your tables ant it'll go smooth