MySQL change data set with table JOIN .. Is what I am asking possible? - mysql
This is a complicated question, so please bear with me. I am using 3 different tables to make 1 result set. They are as follows:
customer_address_entity
entity_id | entity_type_id | attribute_set_id | increment_id | parent_id | create_at | update_at | is_active
customer_entity_int
value_id | entity_type_id | attribute_id | entity_id | value
customer_address_entity_varchar
value_id | entity_type_id | attribute_id | entity_id | value
Ok, so now you have the structure, here is my SQL call I have built so far:
SELECT CAE.entity_id,
CEI.value AS default_entity_id,
CAEV.attribute_id,
CAEV.value
FROM customer_address_entity AS CAE
JOIN customer_entity_int AS CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = '13'
JOIN customer_address_entity_varchar AS CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = '2328'
AND CAE.is_active = 1
This outputs the following example dataset:
ID default att value
'1567', '1567', '19', 'John'
'1567', '1567', '21', 'Doe'
'1567', '1567', '23', 'Johns Company'
'1567', '1567', '25', 'Johns City'
'1567', '1567', '26', 'Johns Country'
'1567', '1567', '27', 'Johns State'
'1567', '1567', '29', 'Johns Zip Code'
'1567', '1567', '30', 'Johns Phone'
'1567', '1567', '31', 'Johns Fax'
'1568', '1567', '19', 'Jane'
'1568', '1567', '21', 'Doe'
'1568', '1567', '23', 'Janes Company'
'1568', '1567', '25', 'Janes City'
'1568', '1567', '26', 'Janes Country'
'1568', '1567', '27', 'Janes State'
'1568', '1567', '29', 'Janes Zip'
'1568', '1567', '30', 'Janes Phone'
'1568', '1567', '31', 'Janes Fax'
'1569', '1567', '19', 'Frank'
'1569', '1567', '21', 'Frunz'
'1569', '1567', '23', 'Franks Company'
'1569', '1567', '25', 'Franks City'
'1569', '1567', '26', 'Franks Country'
'1569', '1567', '27', 'Franks State'
'1569', '1567', '29', 'Franks Zip'
'1569', '1567', '30', 'Franks Phone'
'1569', '1567', '31', 'Franks Fax'
The final part of this code, I would like to create X number (in this case 3) of ROWS based on the number UNIQUE entity_id (Column 1 in returned data set ie .1567,1568 and 1569). The intended end result being:
'1567', '1567', 'John', 'Doe', 'Johns Company', 'Johns City', 'Johns State', 'Johns Zip Code', 'Johns Phone', 'Johns Fax'
'1568', '1567', 'Jane', 'Doe', 'Janes Company', ... etc
'1569', '1567', 'Frank', 'Franz', 'Franks Comapny', ... etc
Is this even possible?
EDIT Thanks to Gordon Linoff -- The answer is elegant and simple! I threw in a few edits of my own, but will be accepting Gordons answer and voting it up. Here are the edits I made, which work beautifully!!
select entity_id,
if(entity_id = default_entity_id, 'true', 'false') as default_entity,
max(case when attr = '19' then `value` end) as `FirstName`,
max(case when attr = '21' then `value` end) as `LastName`,
max(case when attr = '23' then `value` end) as `CompanyName`,
max(case when attr = '25' then `value` end) as `City`,
max(case when attr = '27' then `value` end) as `State`,
max(case when attr = '29' then `value` end) as `ZipCode`,
max(case when attr = '30' then `value` end) as `PhoneNumber`,
max(case when attr = '31' then `value` end) as `Fax`
from (SELECT CAE.entity_id, CEI.value AS default_entity_id, CAEV.attribute_id AS attr, CAEV.value
FROM customer_address_entity CAE
JOIN customer_entity_int CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = '13'
JOIN customer_address_entity_varchar CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = '2328'
AND CAE.is_active = 1
) as t
group by entity_id
You can do this with a group by:
select entity_id,
MAX(default) as default,
max(case when att = '19' then value end) as FirstName,
max(case when att = '21' then value end) as LastName,
max(case when att = '23' then value end) as CompanyName,
max(case when att = '25' then value end) as City,
max(case when att = '27' then value end) as State,
max(case when att = '29' then value end) as ZipCode,
max(case when att = '30' then value end) as PhoneNumber,
max(case when att = '31' then value end) as Fax
from (SELECT CAE.entity_id, CEI.value AS default_entity_id, CAEV.attribute_id, CAEV.value
FROM customer_address_entity CAE
JOIN customer_entity_int CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = '13'
JOIN customer_address_entity_varchar CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = '2328'
AND CAE.is_active = 1
) t
group by entity_id
This process is called pivoting and aggreagtion is one solution (some databases have a pivot keyword for this). This assumes that each value appears only once per entity. Also, if a value is not present, it will get the value NULL.
As #Gordon's answer points out, this is what is known as a PIVOT but MySQL does not have that function. In MySQL you can use an aggregate function with a CASE statement.
You can hard-code the values if they are all known:
SELECT CAE.entity_id,
CEI.value AS default_entity_id,
MAX(case when CAEV.attribute_id = 19 then CAEV.value else null end) FirstName,
MAX(case when CAEV.attribute_id = 21 then CAEV.value else null end) LastName,
MAX(case when CAEV.attribute_id = 23 then CAEV.value else null end) Company,
MAX(case when CAEV.attribute_id = 25 then CAEV.value else null end) City,
MAX(case when CAEV.attribute_id = 26 then CAEV.value else null end) Country,
MAX(case when CAEV.attribute_id = 27 then CAEV.value else null end) State,
MAX(case when CAEV.attribute_id = 29 then CAEV.value else null end) ZipCode,
MAX(case when CAEV.attribute_id = 30 then CAEV.value else null end) Phone,
MAX(case when CAEV.attribute_id = 31 then CAEV.value else null end) Fax
FROM customer_address_entity AS CAE
JOIN customer_entity_int AS CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = '13'
JOIN customer_address_entity_varchar AS CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = '2328'
AND CAE.is_active = 1
GROUP BY CAE.entity_id, CEI.value;
Or you can use a prepared statement to implement dynamic sql. I would assume that you have a table that ties each attribute value to the name of the attribute:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when CAEV.attribute_id = ''',
attribute_id,
''' then CAEV.value else null end) AS `',
attribute_id, '`'
)
) INTO #sql
FROM customer_address_entity_varchar;
SET #sql = CONCAT('SELECT CAE.entity_id,
CEI.value AS default_entity_id, ', #sql, '
FROM customer_address_entity AS CAE
JOIN customer_entity_int AS CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = ''13' '
JOIN customer_address_entity_varchar AS CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = ''2328''
AND CAE.is_active = 1
GROUP BY CAE.entity_id, CEI.value');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Related
SQL query to get number of clients with last statement equal connected
I need to make a SQL query table 'records' structure: contact_id(integer), client_id(integer), worker_id(integer), statement_status(varchar), contact_ts(timestamp) It has to show the following: current date number of clients which last statement_status was 'interested' number of clients which last statement_status was 'not_interested' and previus status was 'not_present' Could somebody help? sample data: contact_id client_id contact_ts worker_id statement_status '1', '181', '2017-09-24 03:38:31.000000', '107', 'voicemail' '2', '72', '2017-09-23 09:32:38.000000', '10', 'not_interested' '3', '277', '2017-09-22 07:06:16.000000', '119', 'interested' '4', '36', '2017-09-21 04:39:57.000000', '118', 'not_present' '5', '33', '2017-09-20 04:12:12.000000', '161', 'voicemail' '6', '244', '2017-09-19 02:26:30.000000', '13', 'not_interested' '7', '346', '2017-09-18 02:30:35.000000', '255', 'interested' '8', '128', '2017-09-17 06:20:13.000000', '52', 'not_present' '9', '33', '2017-09-16 08:58:02.000000', '188', 'not_present' '10', '352', '2017-09-15 08:18:40.000000', '324', 'not_interested' '11', '334', '2017-09-14 04:27:40.000000', '373', 'interested' '12', '2', '2017-09-13 08:44:40.000000', '40', 'not_present' '13', '33', '2017-09-12 03:46:16.000000', '252', 'voicemail' '14', '366', '2017-09-11 04:31:22.000000', '78', 'not_interested' '15', '184', '2017-09-10 06:08:01.000000', '289', 'interested' '16', '184', '2017-09-09 05:45:56.000000', '124', 'not_present' '17', '102', '2017-09-08 07:09:30.000000', '215', 'voicemail' '18', '140', '2017-09-07 08:09:18.000000', '196', 'not_interested' '19', '315', '2017-09-06 05:13:40.000000', '242', 'interested' '20', '268', '2017-09-05 07:41:40.000000', '351', 'not_present' '21', '89', '2017-09-04 05:32:05.000000', '232', 'voicemail' desired output: Time, interested, not-interested 2017-09-10 06:08:01, 5, 5 I tried something with sub queries, but it obviously doesn't work: SELECT GETDATE() ,(select count(*) from record a where (select statement_status from record where client_id == a.client_id order by a.contact_ts limit 1) == "interested" group by a.contact_id) ,(select count(*) from record a where (select (select statement_status from record where client_id == a.client_id order by a.contact_ts limit 2) order by a.contact_ts desc limit 1) == "interested" and (select statement_status from record where client_id == a.client_id order by a.contact_ts limit 1) == "interested" group by a.contact_id) from record b; How should I use the inner selects? I must write a poem, because most of my post is a code. So maybe something from "Dead man"? “Don't let the sun burn a hole in your ass, William Blake. Rise now, and drive your cart and plough over the bones of the dead!” ;)
Try something like this: WITH status AS ( SELECT DISTINCT client_id, first_value(statement_status) OVER w1 AS last_status, nth_value(statement_status, 2) OVER w1 AS prev_status FROM records WINDOW w1 AS (PARTITION BY client_id ORDER BY contact_ts DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) ) SELECT CURRENT_DATE(), SUM(last_status = 'interested') AS interesed, SUM(last_status = 'not_interested' AND prev_status = 'not_present') AS not_interested FROM status
MySQL count(*) returning 0 even though I used IFNULL and COALESCE
Here is my query: USE adventureWorks4mysql; SELECT DISTINCT a.city, count(a.city) as "City Count", emp.Gender as Gender, emp.VacationHours as VacationHours, (select if(count(*) is null,0, count(*)) FROM address aa inner join employeeaddress empad on aa.AddressID = empad.AddressID inner join employee emp on empad.EmployeeID = emp.EmployeeID where MaritalStatus = 'M' and aa.city = a.city group by aa.City) as married, (select ifnull(count(*),0) FROM address aa inner join employeeaddress empad on aa.AddressID = empad.AddressID inner join employee emp on empad.EmployeeID = emp.EmployeeID where MaritalStatus = 'S' and aa.city = a.city group by aa.City) as single FROM address a inner join employeeaddress empad on a.AddressID = empad.AddressID inner join employee emp on empad.EmployeeID = emp.EmployeeID group by a.City; returns the following: 'Bellevue', '36', 'F', '5', '22', '14' 'Berlin', '1', 'F', '35', NULL, '1' 'Bordeaux', '1', 'M', '34', NULL, '1' 'Bothell', '13', 'M', '9', '7', '6' 'Calgary', '1', 'M', '33', '1', NULL 'Cambridge', '2', 'F', '37', '2', NULL 'Carnation', '5', 'M', '77', '4', '1' 'Detroit', '1', 'M', '38', NULL, '1' 'Duluth', '1', 'F', '24', NULL, '1' 'Duvall', '10', 'F', '80', '3', '7' 'Edmonds', '25', 'M', '84', '16', '9' 'Everett', '18', 'M', '42', '11', '7' 'Gold Bar', '5', 'M', '92', '3', '2' 'Index', '5', 'F', '61', '3', '2' 'Issaquah', '15', 'M', '70', '4', '11' 'Kenmore', '12', 'F', '86', '5', '7' 'Kent', '1', 'F', '5', '1', NULL 'Melbourne', '1', 'F', '36', NULL, '1' 'Memphis', '1', 'M', '29', '1', NULL 'Minneapolis', '1', 'M', '48', NULL, '1' 'Monroe', '14', 'M', '21', '4', '10' 'Nevada', '1', 'F', '27', '1', NULL 'Newport Hills', '7', 'M', '44', '2', '5' 'Ottawa', '1', 'M', '31', '1', NULL 'Portland', '1', 'F', '22', NULL, '1' 'Redmond', '21', 'M', '2', '11', '10' 'Renton', '17', 'M', '6', '12', '5' 'Sammamish', '17', 'F', '31', '6', '11' 'San Francisco', '2', 'M', '16', '2', NULL 'Seattle', '44', 'F', '82', '21', '23' 'Snohomish', '10', 'M', '88', '3', '7'
Not at all clear about you desired result, but is you are attempting to count cities, then I suggest you use "conditional aggregates" instead of your current approach, like this: SELECT a.city , COUNT( a.city ) AS "City Count" , count(CASE WHEN maritalstatus = 'M' THEN a.city END) AS married , count(CASE WHEN maritalstatus = 'S' THEN a.city END) AS single FROM address a INNER JOIN employeeaddress empad ON a.addressid = empad.addressid INNER JOIN employee emp ON empad.employeeid = emp.employeeid GROUP BY a.city; Note how the case expression is INSIDE the aggregate function COUNT - hence the term "conditional aggregates" e.g. for singles, if there is a singe status then count that address other wise just ignore that row. nb COUNT does not increment if a value is null. Please also note that you are only grouping by the single column city. If you really want more result rows because of gender and vacationhours then also use those columns in the GROUP BY clause SELECT a.city , emp.gender AS Gender , emp.vacationhours AS VacationHours , COUNT( a.city ) AS "City Count" , count(CASE WHEN maritalstatus = 'M' THEN a.city END) AS married , count(CASE WHEN maritalstatus = 'S' THEN a.city END) AS single FROM address a INNER JOIN employeeaddress empad ON a.addressid = empad.addressid INNER JOIN employee emp ON empad.employeeid = emp.employeeid GROUP BY a.city , emp.gender , emp.vacationhours ;
SELECT from table AND 'archive' table at once
To reduce the size of one of our tables (currently over 4 million rows), I've created an 'archive' table which will only be accessed by certain users. The archive table is a replica of the main table, except it has OLD rows in it that are rarely accessed. However, sometimes I'll need to query both tables: SELECT t1.*, t6.* FROM ( SELECT COUNT(DISTINCT up.lesson_id) AS assessment_count, MIN(up.date) AS first_date, MAX(up.date) AS last_date FROM cdu_user_progress up WHERE (up.game_id = '0') AND (up.uid = '150') AND (up.lesson_id IN ('65', '1112', '66', '67', '68', '69', '1114', '70', '71', '72', '73', '74', '75', '1113', '77', '424', '423', '1115', '93', '94', '95', '420', '421', '422', '96', '97', '98', '99', '100', '101', '1015', '102', '415', '104', '106', '105', '107', '108', '1016', '109', '110', '160', '111', '113', '112', '738', '739', '1050', '1051', '116', '117', '118', '119', '120', '121', '1017', '123', '124', '125', '130', '1018', '131', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '267', '268', '269', '270', '271', '272', '273', '274', '1019', '278', '775', '279', '280', '281', '282', '283', '284', '285', '286', '1161', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '318', '319', '302', '303', '304', '305', '306', '307', '308', '309', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '1054', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '377', '378', '379', '380', '479', '480', '481', '482', '431', '432', '433', '434', '435', '436', '437', '438', '439', '440', '441', '442', '443', '444', '445', '446', '448', '447', '449', '450', '451', '452', '453', '454', '456', '455', '457', '458', '459', '460', '461', '462', '463', '464', '465', '466', '467', '468', '469', '470', '471', '472', '473', '474', '475', '476', '477', '478', '1090')) AND (up.score > '-1') ) t1 INNER JOIN ( SELECT AVG(c1.score) AS avg_first_scores, SUM(c1.score) AS sum_first_scores FROM cdu_user_progress c1 LEFT OUTER JOIN cdu_user_progress c2 ON c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1 WHERE (c1.game_id = '0') AND (c1.uid = '150') AND (c1.lesson_id IN ('65', '1112', '66', '67', '68', '69', '1114', '70', '71', '72', '73', '74', '75', '1113', '77', '424', '423', '1115', '93', '94', '95', '420', '421', '422', '96', '97', '98', '99', '100', '101', '1015', '102', '415', '104', '106', '105', '107', '108', '1016', '109', '110', '160', '111', '113', '112', '738', '739', '1050', '1051', '116', '117', '118', '119', '120', '121', '1017', '123', '124', '125', '130', '1018', '131', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '267', '268', '269', '270', '271', '272', '273', '274', '1019', '278', '775', '279', '280', '281', '282', '283', '284', '285', '286', '1161', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '318', '319', '302', '303', '304', '305', '306', '307', '308', '309', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '1054', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '377', '378', '379', '380', '479', '480', '481', '482', '431', '432', '433', '434', '435', '436', '437', '438', '439', '440', '441', '442', '443', '444', '445', '446', '448', '447', '449', '450', '451', '452', '453', '454', '456', '455', '457', '458', '459', '460', '461', '462', '463', '464', '465', '466', '467', '468', '469', '470', '471', '472', '473', '474', '475', '476', '477', '478', '1090')) AND (c1.score > '-1') AND (c2.date IS '') ) t6 The solutions I've already thought of - replace SELECT cdu_user_progress with a nested-query: SELECT * FROM cdu_user_progress UNION SELECT * FROM cdu_user_progress_archive But this would create a huge temporary table in memory. I tried UNION of each query (nested queries above), but this results in multiple rows returned, where it used to return a single row. SELECT ... FROM ( SELECT ... FROM cdu_user_progress up WHERE ... UNION SELECT ... FROM cdu_user_progress_archive up WHERE ... ) t1 INNER JOIN ( SELECT ... FROM cdu_user_progress c1 LEFT OUTER JOIN cdu_user_progress c2 ON c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1 WHERE ... UNION SELECT ... FROM cdu_user_progress_archive c1 LEFT OUTER JOIN cdu_user_progress_archive c2 ON c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1 WHERE ... ) t6 Any other ideas??? Thanks!
Those suq-queries contain aggregates. So a simple solution is to sin against the DRY principle (Don't Repeat Yourself). I.e. copy&paste those queries, change to the archive tables and union them. But that list of lesson_id's is rather long. Probably better to put those in a temp table so it can be reused. SELECT t1.*, t6.* FROM ( SELECT 'current' as Src, COUNT(DISTINCT up.lesson_id) AS assessment_count, MIN(up.date) AS first_date, MAX(up.date) AS last_date FROM cdu_user_progress up INNER JOIN temp_lessonid_list lst ON lst.lesson_id = up.lesson_id WHERE up.game_id = 0 AND up.uid = 150 AND up.score > -1 UNION ALL SELECT 'archive' as Src, COUNT(DISTINCT up.lesson_id) AS assessment_count, MIN(up.date) AS first_date, MAX(up.date) AS last_date FROM cdu_user_progress_archive up INNER JOIN temp_lessonid_list lst ON lst.lesson_id = up.lesson_id WHERE up.game_id = 0 AND up.uid = 150 AND up.score > -1 ) t1 INNER JOIN ( SELECT 'current' as Src, AVG(c1.score) AS avg_first_scores, SUM(c1.score) AS sum_first_scores FROM cdu_user_progress c1 INNER JOIN temp_lessonid_list lst ON lst.lesson_id = c1.lesson_id LEFT JOIN cdu_user_progress c2 ON (c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1) WHERE c1.game_id = 0 AND c1.uid = 150 AND c1.score > -1 AND c2.date IS '' UNION ALL SELECT 'archive' as Src, AVG(c1.score) AS avg_first_scores, SUM(c1.score) AS sum_first_scores FROM cdu_user_progress_archive c1 INNER JOIN temp_lessonid_list lst ON lst.lesson_id = c1.lesson_id LEFT JOIN cdu_user_progress_archive c2 ON (c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1) WHERE c1.game_id = 0 AND c1.uid = 150 AND c1.score > -1 AND c2.date IS '' ) t6 ON t6.Src = t1.Src; You could also research table partitioning.
you can write 2 separate queries for each table and use the mysql UNION keyword to join them together eg (SELECT * FROM table1 WHERE col="value") UNION (SELECT * FROM table2 WHERE col="value"); https://dev.mysql.com/doc/refman/8.0/en/union.html
mysqli query for multiple cases with coditions
Need to select data if condition first true then check user id exist, if condition not true then check condition two then check user id not exist Technologies used: Codigniter - Mysqli SELECT * FROM `tablename` WHERE `user_id` = '1' OR `user_id` IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3') AND `user_id` NOT IN('35') OR (CASE WHEN except_friends = 1 THEN FIND_IN_SET('1', friends_list), FALSE END CASE WHEN selected_friends = 1 THEN NOT FIND_IN_SET('1', friends_list), FALSE END) ORDER BY `id` desc Mysql syntax error
I think there is syntax error in your query. try this one SELECT * FROM tablename WHERE user_id = '1' OR user_id IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3') AND user_id NOT IN('35') OR (CASE WHEN except_friends = 1 THEN NOT FIND_IN_SET('1', friends_list) WHEN selected_friends = 1 THEN NOT FIND_IN_SET('1', friends_list) END) ORDER BY id desc
How do we select non-aggregate columns in a query with a GROUP BY clause, which is not functionally dependent on columns in GROUP BY clause?
My SQL SELECT `tbl_order`.`order_id` FROM `tbl_order` LEFT JOIN `tbl_rooms` ON `tbl_order`.`room_name` = `tbl_rooms`.`room_id` LEFT JOIN `tbl_orderdetails` ON `tbl_order`.`order_key` = `tbl_orderdetails`.`orderdetail_orderkey` LEFT JOIN `tbl_guestcatlang` ON `tbl_order`.`order_product_type`=`tbl_guestcatlang`.`guestcatlang_guestcat_id` and `tbl_guestcatlang`.`guestcatlang_lang_id`="en" JOIN `tbl_sitesettings` ON `tbl_order`.`order_setting_id` = `tbl_sitesettings`.`setting_id` WHERE ( ( tbl_sitesettings.delayedBrekfast = 1 AND `tbl_order`.`order_product_type` = "BREAKFAST" AND ( `tbl_order`.`order_required_time` > "2018-02-28 18:30:00" AND `tbl_order`.`order_required_time` < "2018-03-01 18:29:59" AND tbl_sitesettings.delayTime - (unix_timestamp(convert_tz(now(),("UTC"), tbl_sitesettings.timeZone))-unix_timestamp()) < "26100" ) ) OR ( `tbl_sitesettings`.`delayedBrekfast` != 1 OR `tbl_order`.`order_status` = "Complete" OR tbl_order.order_product_type != "BREAKFAST" ) ) AND `tbl_order`.`order_status` IN('Open') AND `tbl_order`.`order_product_type` IN('BREAKFAST', 'Room Service', 'AMENITIES', 'GIFT SHOP', 'Internet Plan', 'Device Management', 'System Message', '70', '69', '67', '68', '75', '76', '77', '78', '93', '94', '95', '97', '98', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '127', '128', '135') AND `tbl_order`.`order_product_type` != 'Recomended Product' AND `tbl_order`.`order_setting_id` = '7' GROUP BY `tbl_order`.`order_key` ORDER BY `tbl_order`.`ordered_time` DESC And this was the error Message Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ebdb_production.tbl_order.order_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by Any solution ??? :(
Try execute the below query. This will remove the restriction of such. SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));