This question already has answers here:
Get Identity after selecting from distinct result
(3 answers)
Closed 10 years ago.
i have 2 tables ( Model_Table , Items_Table)
Model_Tabl ( ID, ModelName, ModelQuantity)
Items_Tabl ( I_Code, IName, ID)
after inserting new row into (Model_Table) - Triggers insert multi row into (Items_Table) Depend on ModelQuantity from (Model_Table) , and until now its work fine
I Created
"select distinct ModelName , Sum(ModelQuantity) group by ModelName"
and i got result fine
My question is :
When i select model name from (DISTINCT) query i want to know which (ID) I selected from (Model_Table)
(Model_ID) to (ModelName) = 1 to many
ty
just select also the id ,try this
select id, ModelName , Sum(ModelQuantity)
from Model_Tabl group by ModelName
you dont need DISTINCT because you already making group by and they do same work
Related
This question already has answers here:
You can't specify target table for update in FROM clause
(11 answers)
MySQL DELETE FROM with subquery as condition
(9 answers)
Closed 9 months ago.
I'm doing a function that removes a project, and when it removes its tags are removed if there isn't another projects using the same tag.
So when I select the project it sends this:
delete from Project_Tag where (TagId, ProjectId) in (
select q.ProjectId, q.TagId from Project_Tag q
where q.TagId in (select g.TagId
from Project_Tag g where ProjectId = <ID OF THE PROJECT>)
group by q.TagId having count(q.TagId) = 1
);
But it gives an error: You can't specify target table "Project_Tag" for update in FROM clause
This select is working, but I can't delete it
select q.ProjectId, q.TagId from Project_Tag q
where q.TagId in (select g.TagId
from Project_Tag g where ProjectId = <ID OF THE PROJECT>)
group by q.TagId having count(q.TagId) = 1
Is there another way to solve this? I'm newbie yet :P
This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I went through the previous answer like this, but it gives me the following error : You can't specify target table 'table_name' for update in FROM clause.
I have a table with say 3 columns (id -> auto increment primary id) :
id, roll_no and attendance
And for selected roll numbers having many entries each, except the first entry I want to update all entry attendance field as P.
The query which I wrote is following :
UPDATE tbl_class_attendance
set attendance = 'P'
where id NOT IN
(Select min(id)
from tbl_class_attendance
WHERE roll_no IN ('25', '45', '55')
GROUP
BY roll_no;
But it gives me the above error.
I also went through other answers asking to use two select queries but there the answer I didn't find completely easy to understand as well as difficulty in executing for my selected list of roll numbers.
So, is there a way to update?
EDIT : Answer given below
UPDATE tbl_class_attendance t1
LEFT JOIN ( SELECT t2.roll_no, MIN(t2.id) id
FROM tbl_class_attendance t2
WHERE t2.roll_no IN ('25', '45', '55')
GROUP BY t2.roll_no ) t3 USING (roll_no, id)
SET t1.attendance = 'P'
WHERE t3.id IS NULL;
Got it working by following :
Update table SET a=value WHERE x IN
(Select * from (select x from table where condition) as t)
Credit : https://stackoverflow.com/a/43610081/6366458
This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 4 years ago.
I have these tables :
work --> id, name, description
subwork --> id, name , work_id
which work_id is foreign key for work table .
Im using this query to get results :
("select work.*, subwork.name from work"
"inner join subwork on subwork.work_id = work.id")
this will return the repetitive values , on the other hand I need all of data in result so distinct is not helpful . I wanna know is there any way to get results like this :
(work_id, work_name, work_desc, (subwork_name1, subwork_name2, subwork_name3,... ))
You can get it as a string, if you use group_concat(). Maybe that helps too.
SELECT w.id,
w.name,
w.description,
group_concat(s.name SEPARATOR ', ')
FROM work w
INNER JOIN subwork s
ON s.work_id = w.id
GROUP BY w.id,
w.name,
w.description;
This question already has answers here:
SELECT SUM returns a row when there are no records
(8 answers)
Closed 7 years ago.
i want to execute this query but it return null row when the table empty. it's working when SUM(products.sale_price)/COUNT(orders.id) AS avg_price_rang
is not in the query
SELECT
products.brand_id,
(SELECT
category_brands.name
FROM
category_brands
WHERE
category_brands.id=products.brand_id
) AS brand,
products.material_id,
(SELECT
category_materials.material
FROM
category_materials
WHERE
category_materials.id=products.material_id
) AS material,
orders.color_code,
SUM(products.sale_price)/COUNT(orders.id) AS avg_price_rang
FROM
orders
INNER JOIN
products
ON
orders.prodcut_id = products.id
I think it's missing GROUP BY products.brand_id.
SELECTing SUM when there are no rows causess a row of all NULLs to be returned.
MySQL 5.5 Reference Manual
12.17.1 GROUP BY (Aggregate) Functions
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows. For more information, see Section 12.17.3, “MySQL Handling of GROUP BY”.
Use:
query
HAVING avg_price_rang IS NOT NULL
SELECT * FROM (
query
) dummy
WHERE avg_price_rang IS NOT NULL
This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 9 years ago.
I have a table and I need the correct statement to delete duplicate records. I want to achieve this by deleting rows in the table where multiple column values equal those of other records in the same table. I can do simple delete from [table] where [col] = [val] but I have no idea how to do this. Can you provide a template that I can work from?
Thanks.
delete from [table]
where [col] not in
(
select * from
(
select min([col])
from [table]
group by [col]
having count(distinct [val]) <> count([val])
) x
)
If you select from a table you are deleting from in MySQL then you have to use a subquery to hide that. That is why I used the select * from (...) x
Assuming that the original key's id is say 7,
DELETE FROM [table] WHERE [col1] = [val1] AND [col2] = [val2] .... AND [id] != 7;