This question already has answers here:
MySQL : isn't in GROUP BY
(5 answers)
Closed 3 years ago.
I have the following query:
SELECT cn.id, cn.title, cn.type, cn.status FROM (
SELECT v.concert_id as concert_id, SUM(v.position_votes + v.support_votes) as votes
FROM (
SELECT q.concert_id as concert_id, q.position_id as position_id, q.position_votes as position_votes, SUM(q.votes_up + q.votes_down) as support_votes
FROM (
SELECT qcs.id AS concert_id, p.id AS position_id, p.votes AS position_votes, IF(s.votes_in_favor <=> null, 0, s.votes_in_favor) AS votes_up, IF(s.votes_not_in_favor <=> null, 0, s.votes_not_in_favor) AS votes_down
FROM (
SELECT concert_id AS id
FROM positions p
WHERE p.content LIKE '%%'
GROUP BY concert_id
)
AS qcs
JOIN positions p
ON p.concert_id = qcs.id
LEFT JOIN supports s
ON s.position_id = p.id
) AS q
GROUP BY q.position_id
) as v
GROUP BY v.concert_id
) as r
JOIN concerts cn on cn.id = r.concert_id
ORDER BY r.votes DESC, cn.created_at DESC
When I made that query directly into MySQL I get the desired results. But when I put that query using DB, for example:
$query = "...alll_the_previous_query";
$result = DB::select(DB::raw($query));
I got the following error:
local.ERROR: SQLSTATE[42000]: Syntax error or access violation: 1055 'q.concert_id' isn't in GROUP BY ...the rest of the query
I know that a way to avoid this is to change the database configuration in Laravel and change the strict to false.
But that is not an option.
What is wrong with my query when I passed it to Laravel?
You can add the q.concert_id to the group by. Instead of:
GROUP BY q.position_id
Use:
GROUP BY q.concert_id, q.position_id
Related
I have to get results from different tables in one query. But got the unknown column error.
SET sql_mode = '';
SELECT
e_mills.m_id,
e_mills.mill_name,
e_cities.city_name,
e_sugardata.mill_closing_stock,
(
SELECT GROUP_CONCAT(mill_closing_stock SEPARATOR ', ')
FROM (
SELECT mill_closing_stock
FROM `e_sugardata`
WHERE m_id = e_mills.m_id
GROUP BY date_added
ORDER BY date_added DESC
LIMIT 0,4
) AS mill_closing_stock
) AS stock_chart
FROM e_mills
INNER JOIN e_cities
ON e_mills.city_id = e_cities.city_id
INNER JOIN e_sugardata
ON e_sugardata.m_id = e_mills.m_id
ORDER BY e_mills.province_id
ERROR
#1054 - Unknown column 'e_mills.m_id' in 'where clause'
UPDATE
Same query is working on my computer but not on other computer
UPDATE 2 working on MySQL 8
MySQL 8 DB Fiddle
To avoid an error in MySQL 5.7, you need to get rid of the nested query with a limit. Instead of a LIMIT clause, you can use the SUBSTRING_INDEX function. Also, the GROUP BY clause is unnecessary, especially since it does not work without changing sql_mode if the column list of the SELECT clause does not match the GROUP BY list.
-- SET sql_mode = ''; -- no more needed.
SELECT
e_mills.m_id,
e_mills.mill_name,
e_cities.city_name,
e_sugardata.mill_closing_stock,
(
SELECT
SUBSTRING_INDEX(
GROUP_CONCAT(mill_closing_stock ORDER BY date_added DESC SEPARATOR ', '
), ', ', 4
)
FROM `e_sugardata`
WHERE m_id = e_mills.m_id
) AS stock_chart
FROM e_mills
INNER JOIN e_cities
ON e_mills.city_id = e_cities.city_id
INNER JOIN e_sugardata
ON e_sugardata.m_id = e_mills.m_id
ORDER BY e_mills.province_id
db<>fiddle: MySQL 5.7 and MySQL 8
This question already has answers here:
SELECT DISTINCT and ORDER BY in MySQL
(2 answers)
Closed 3 years ago.
I have two tables clients & exchange. I want to get clients id by running left join query. But when I run the query it throws an error.
select distinct
`clients`.`id` as `client_id`
from `clients`
left join `exchange`
on `clients`.`id` = `exchange`.`client_id`
where
`clients`.`iex_status` = 'Active'
order by
`exchange`.`validity_to` desc
error:
add exchange.validity_to this column into selection otherwise it will throw this error validity_to is not in SELECT list; this is incompatible with DISTINCT
select distinct
clients.id as client_id,exchange.validity_to
from clients
left join exchange
on clients.id = exchange.client_id
where
clients.iex_status = Active
order by
exchange.validity_to desc
But from your screen shot it seems you used parenthesis after distinct thats why you got the error
Hope you doing well, i have checked your query but found 2 diff. points
1 : "select distinct clients.id as client_id from clients left join exchange on clients.id = exchange.client_id where clients.iex_status = 'Active' order by exchange.validity_to desc "
You show this query in first and showing error in your query
2 : you are using "(" in your query that you have executed in sql i think your first query is right that will give you right result.
so please do not use ( ) in distinct fields.
thanks
As per your image attached you can modify alias out of braces as -
select distinct
(`clients`.`id` as `client_id`) as 'client_id'
from `clients`
left join `exchange`
on `clients`.`id` = `exchange`.`client_id`
where
`clients`.`iex_status` = 'Active'
order by
`exchange`.`validity_to` desc
I am having trouble with a MySQL query. The query is as follows:
SET #catlocation = (SELECT id FROM categories WHERE url_name='hexcode');
SELECT
subs.display_name AS display,
subs.url_name AS url,
(
SELECT title
FROM threads
WHERE location = subs.id
ORDER BY time_submitted DESC
LIMIT 1
) AS title,
(
SELECT username
FROM users
WHERE uid = (
SELECT uid
FROM threads
WHERE location = subs.id
ORDER BY time_submitted DESC
LIMIT 1
)
LIMIT 1
) AS author,
(
SELECT COUNT(*)
FROM threads
WHERE location = subs.id
ORDER BY time_submitted DESC
LIMIT 1
) AS thread_count
FROM (
SELECT *
FROM categories
WHERE parent_id = #catlocation
) AS subs
When I try to run this through PHP I get a false result and an error of:
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 'SELECT subs.display_name AS display, subs.url_name AS url, ( SELECT threads.' at line 7
I have no idea what the syntax error could be, if someone could point it out to me that would be wonderful.
EDIT: Could this be caused by having two select statements (The one that sets #catlocation and the main query?)
You can refactor your request with joins to increase performance.
SELECT s.display_name display, s.url_name url,
t1.title, u.username author,
COUNT(t2.title) total
FROM categories s
LEFT JOIN threads t1 ON t1.id = (SELECT id FROM threads
WHERE location = s.id
ORDER BY time_submitted DESC
LIMIT 1)
LEFT JOIN users u ON u.uid = t1.uid
LEFT JOIN threads t2 ON t2.location = s.id
WHERE s.parent_id = #catlocation
GROUP BY s.display_name, s.url_name, t1.title, u.username
In a ansi SQL you need to declare a tag for each table or omit it if there is only one. Try taking out the "threads." everywhere, it is not needed
It appears the first SELECT statement which set #catlocation was causing the problem. I moved it into a subquery and the query executed successfully
The new query is as follows:
SELECT categories.display_name display,
categories.url_name url,
threads.title title,
users.username author,
( SELECT COUNT(title)
FROM threads
WHERE location = categories.id
) total
FROM categories
LEFT JOIN threads
ON threads.tid = ( SELECT tid
FROM `threads`
WHERE location = categories.id
ORDER BY time_submitted DESC
LIMIT 1 )
LEFT JOIN users ON users.uid = threads.uid
WHERE categories.parent_id = ( SELECT id
FROM `categories`
WHERE url_name='hexcode'
LIMIT 1 );
I will continue to refactor the query by using JOINs (once I learn how to use them). Thanks to all that suggested fixes, I didn't understand the JOIN answer and still couldn't get it to run without error.
This question already has answers here:
Operand Should Contain 1 Column - MySQL NOT IN
(3 answers)
Closed 8 years ago.
I have an sql query that was returning some incorrect results due to product id's being duplicated for linked products, so i added the following to the end of my query expecting it to only take the first instance of any product id:
AND pc.products_id
IN
(SELECT
pc.products_id, MIN(categories_id)
FROM
zen_products_to_categories pc
GROUP BY
pc.products_id)
But i get an Operand should contain 1 column(s) error when i run the process. I ran that query on it's own and it only gave me each product id once, so not sure why i get the error.
The full query i now have is:
SELECT p.products_quantity, p.abebooks_status,
p.products_id AS id,
p.products_status AS prodStatus,
FORMAT( IFNULL(s.specials_new_products_price, p.products_price),2) AS price,
pc.categories_id AS prodCatID,
c.parent_id AS catParentID,
cd.categories_name AS catName
FROM
zen_products p
JOIN zen_products_description pd ON p.products_id=pd.products_id
JOIN zen_products_to_categories pc ON p.products_id=pc.products_id
JOIN zen_categories c ON pc.categories_id=c.categories_id
JOIN zen_categories_description cd ON c.categories_id=cd.categories_id
left join zen_specials s on ( s.products_id = p.products_id AND ( (s.expires_date > CURRENT_DATE) OR (s.expires_date = 0) ) )
WHERE p.products_price > 0 and p.products_status = 1
AND pc.products_id
IN
(SELECT pc.products_id, MIN(categories_id)
FROM zen_products_to_categories pc GROUP BY pc.products_id)
ORDER BY catName ASC
Can anyone tell me why it doesn't work when i add the extra query because it's got me baffled
You could try with:
AND (pc.products_id, pc.categories_id)
IN
(SELECT
pc.products_id, MIN(categories_id)
FROM
zen_products_to_categories pc
GROUP BY
pc.products_id)
Edit:
In MySQL a subquery like this is usually slow. You should have better luck with a JOIN:
SELECT ....
FROM
....
INNER JOIN (SELECT
products_id, MIN(categories_id) min_categories_id
FROM
zen_products_to_categories
GROUP BY
products_id) min_ct
ON pc.products_id=min_ct.products_id
AND pc.categories_id=min_ct.min_categories_id
WHERE
....
Subquery should return one column when you are matching against single column remove MIN(categories_id) from your subquery
AND pc.products_id
IN
(SELECT
pc.products_id
FROM
zen_products_to_categories pc
GROUP BY
pc.products_id)
I am trying to move my application from MS SQL Server to MySQL, and I'm facing an issues with MySQL's LIMIT clause in a subquery.
SQL Server code:
select F.call_Id, F.cell_Phone_Number, F.cal_DATE
From TD_Call_Log AS F
Where F.call_Id IN ( Select Top 5 S.call_Id
From TD_Call_Log AS S
where S.cell_Phone_Number = F.cell_Phone_Number)
Order by cell_Phone_Number
MySQL code that I have tried:
select F.call_Id, F.cell_Phone_Number, F.cal_DATE
From TD_Call_Log F
Where F.call_Id IN (Select S.call_Id
From TD_Call_Log S
where S.cell_Phone_Number = F.cell_Phone_Number Limit 5)
Order by cell_Phone_Number
and I have also tried:
select F.call_Id, F.cell_Phone_Number, F.cal_DATE
From TD_Call_Log F INNER JOIN (Select S.call_Id
From TD_Call_Log S
where S.cell_Phone_Number = F.cell_Phone_Number Limit 5) as t
ON F.call_Id = t.call_Id
Order by cell_Phone_Number
Try replacing your subquery within In clause with an Inner join.
select F.call_Id, F.cell_Phone_Number, F.cal_DATE
From TD_Call_Log AS F
INNER JOIN
(
Select S.call_Id, S.cell_Phone_Number
From TD_Call_Log S
Limit 5
)
sub
where sub.cell_Phone_Number = F.cell_Phone_Number and F.call_Id = sub.call_Id
Order by cell_Phone_Number