My query is running well in local server but in live server its getting error.
my local Server version: 10.1.36-MariaDB - mariadb.org binary distribution
and live Server version: 5.7.25-cll-lve - MySQL Community Server - (GPL)
Error message:
In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'uzzal_management.employee.id'; this is incompatible with sql_mode=only_full_group_by
Query:
SELECT `employee`.*,
(SELECT `des_name` FROM `designation`
WHERE `employee`.`des_id` = `designation`.`id`) AS name,
(SELECT `dep_name` FROM `department`
WHERE `employee`.`dep_id` = `department`.`id`) AS dep_name,
`emp_salary`.`total`,
`bank_info`.*,
`addition`.*,
`deduction`.*,
(SELECT TRUNCATE((SUM(ABS(( TIME_TO_SEC( TIMEDIFF( `signin_time`, `signout_time` ) ) )))/3600), 1) AS Hours FROM `attendance`
WHERE (`attendance`.`emp_id`='Gup1410') AND (DATE_FORMAT(`attendance`.`atten_date`, '%m'))=MONTH(CURRENT_DATE())) AS hours_worked,COUNT(*) AS days FROM `employee`
LEFT JOIN `department` ON `employee`.`dep_id`=`department`.`id`
LEFT JOIN `addition` ON `employee`.`em_id`=`addition`.`salary_id`
LEFT JOIN `deduction` ON `employee`.`em_id`=`deduction`.`salary_id`
LEFT JOIN `bank_info` ON `employee`.`em_id`=`bank_info`.`em_id`
LEFT JOIN `emp_salary` ON `employee`.`em_id`=`emp_salary`.`emp_id`
WHERE `employee`.`em_id`='Gup1410'
go to variable and check for sql_mode there you will find out ONLY_FULL_GROUP_BY, remove it and save it. it will work.
Related
I am collecting quote data and chose opt_ticker and quoteTimeStamp as the primary key so that I can store unique quotes over time. I now want to create a view where I can see the latest quote for each opt_ticker (data base has other opt_tickers with unique quotes as well). Basically want to see the latest quote for each stock/option.
In the example above, I want to get that last row as it is the latest timestamp for that particular contract.
I thought this query would do the trick but mysql complains that I need to do a group by.
select symbol,opt_ticker,ask,bid,exp,strike,type,max(quoteTimeStamp)
from optionquotes
group by opt_ticker
21:36:42 select symbol,opt_ticker,ask,bid,exp,strike,type,max(quoteTimeStamp) from optionquotes group by opt_ticker,symbol LIMIT 0, 1000 Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'od2.optionquotes.ask' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 0.000 sec
Here my server info if it helps
Server
Product: (Ubuntu)
Version: 5.7.30-0ubuntu0.16.04.1
Connector
Version: C++ 8.0.20
This sounds so easy but I am having the toughest time figuring this out, thank you in advance.
In MySQL 5.x you can do:
select *
from optionquotes
where (opt_ticker, quoteTimeStamp) in (
select opt_ticker, max(quoteTimeStamp)
from optionquotes
group by opt_ticker
)
In MySQL 8.x you can do:
select *
from (
select *,
row_number() over(partition by opt_ticker order by quoteTimeStamp desc) as rn
from optionquotes
) x
where rn = 1
Just to round out the answers, here is a canonical way to do this using a join:
SELECT oq1.*
FROM optionquotes
INNER JOIN
(
SELECT opt_ticker, MAX(quoteTimeStamp) AS maxQuoteTimeStamp
FROM optionquotes
GROUP BY opt_ticker
) oq2
ON oq1.opt_ticker = oq2.opt_ticker AND
oq1.quoteTimeStamp = oq2.maxQuoteTimeStamp;
I am executing the below query
SELECT DISTINCT n.nid AS entity_id
FROM node n
INNER JOIN og_membership om ON n.nid=om.etid AND om.entity_type='node'
INNER JOIN foster_animal_capacity fc ON n.nid=fc.person_id
LEFT OUTER JOIN field_data_field_attributes fa ON n.nid=fa.entity_id
LEFT OUTER JOIN foster_person_black_outs fb ON n.nid=fb.person_id
WHERE -- n.nid = 1441663 AND
(om.gid = 464) AND (fc.animal_type_id = 3)
AND ((fc.capacity - fc.occupied)>=1)
AND ((fb.start_date IS NULL) OR (fb.end_date < 1523577600) OR (fb.start_date > 1522540800))
AND ((SELECT pid FROM `animal_history` WHERE `TYPE` = 'Foster Return'
AND pid = n.nid ORDER BY DATE_FORMAT(FROM_UNIXTIME( UNIX_TIMESTAMP( ) - MAX( CAST(`time` AS UNSIGNED) ) ) ,'%e')));
The query executing perfectly in MySql 5.5.5
But showing the below error in MySql 5.7.21
ERROR 3029 (HY000): Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
Why is this happening? And how can I overcome this issue?
I am not able to find any documentation for the error code 3029
As mentioned in MySQL documention :
MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default. For a description of pre-5.7.5 behavior, see the MySQL 5.6
For detail please check:
MySQL Handling of GROUP BY
I have this code, and it is working properly on the online server:
SELECT
DATE(`order`.`date`) AS `dater`,
COUNT(*) AS `orders-amount`,
SUM(`order`.`price`) AS `orders-income`,
(SELECT SUM(`amount`) FROM `paypal` WHERE `paypal`.`txn_id` != 'Bonus' AND YEAR(`dater`) = YEAR(`paypal`.`posted_date`) AND MONTH(`dater`) = MONTH(`paypal`.`posted_date`)) AS `total_charge`
FROM `order`
GROUP BY YEAR(`dater`), MONTH(`dater`)
ORDER BY `dater` DESC
But on localhost it gives an error as below:
#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'panel.order.date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Id I used this code for grouping then it working properly:
GROUP BY `dater`
This error appear only on localhost as I'm running on linux Mint and installing the apache, php, mysql & pypmyadmin
It means that different sql modes are used on local and remote servers.
Either you should change the mode or update your query like:
SELECT
YEAR(ANY_VALUE(`order`.`date`)) date_year,
MONTH(ANY_VALUE(`order`.`date`)) date_month,
COUNT(*) AS `orders-amount`,
SUM(`order`.`price`) AS `orders-income`,
(SELECT SUM(`amount`) FROM `paypal` WHERE `paypal`.`txn_id` != 'Bonus' AND YEAR(ANY_VALUE(`order`.`date`)) = YEAR(`paypal`.`posted_date`) AND MONTH(ANY_VALUE(`order`.`date`)) = MONTH(`paypal`.`posted_date`)) AS `total_charge`
FROM `order`
GROUP BY date_year, date_month
You have it in your error message:
this is incompatible with sql_mode=only_full_group_by
If you did not set it on purpose, I guess you got it by default and you have a different version on your local server (see here, it was made default at some point).
So all you need is to disable this mode, either by running SET sql_mode='';, or by following instructions here
I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1
Like this:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
But I get an error with that query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) as ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
I think that's because you can't address a table in the count function.
I can do this using a subquery, but that will likely slowly down the query.
What is a better way of doing this?
You can adress a table column, but you can't address table.*, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip