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;
Related
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 am updating record on the base of sub query but it giving me error
you can't specify target table for update in from clause
my query is
UPDATE paymentinfo set customer_id =
(
SELECT transation.transactionid
FROM paymenttransaction AS transation
LEFT JOIN paymentinfo as payment
ON (transation.paymentinfoid=payment.paymentinfoid)
where payment.hash="0b576d33c57484692131471a847eab7c"
)
WHERE hash="0b576d33c57484692131471a847eab7c"
where am i wrong and what will be perfect solution for that problem
You are updating the table 'paymentinfo' also at same time you are using this table for selection in subquery .
Please break this query in two parts and it will work .
I think it is simplest (in your case) to use the double subquery method:
UPDATE paymentinfo
SET customer_id = (SELECT transactionid
FROM (SELECT t.transactionid
FROM paymenttransaction pt LEFT JOIN
paymentinfo pi
ON t.paymentinfoid = pi.paymentinfoid
WHERE p.hash = '0b576d33c57484692131471a847eab7c'
) t
)
WHERE hash = '0b576d33c57484692131471a847eab7c';
Usually, you want to switch these to use JOIN, but I think that is a bit complicated in this case.
This question already has answers here:
How to return rows that have the same column values in MySql
(3 answers)
Closed 6 years ago.
I am trying to find the rows that exist inside an index, however, my results keep coming up null even though there are rows that match my index. Is there something wrong with the logic of my query?
Here is my test query:
SELECT `the_products` . *
FROM `the_products`
INNER JOIN `producttypes_index` ON `producttypes_index`.`the_product_id` = `the_products`.`id`
AND `producttypes_index`.`type_id` = '1'
INNER JOIN `producthashtags_index` ON `producthashtags_index`.`the_product_id` = `the_products`.`id`
WHERE
`producthashtags_index`.`producthashtag_id`
IN ('41')
AND
`producthashtags_index`.`producthashtag_id`
IN ('42')
AND
`producthashtags_index`.`producthashtag_id`
IN ('6')
ORDER BY updated_at DESC
Here you can see the_product_id 54433 exists inside the index table producthashtags_index using query:
SELECT *
FROM `producthashtags_index`
WHERE `the_product_id` =54433
Results:
id producthashtag_id the_product_id
25433 6 54433
25434 41 54433
25435 42 54433
Then you can see it also exists inside the index table producttypes_index using query:
SELECT *
FROM `producttypes_index`
WHERE `the_product_id` =54433
Results:
type_id the_product_id
1 54433
Remove
AND `producttypes_index`.`type_id` = '1'
you can also optimaze your code by making the 3 IN`s into one like this:
AND `producthashtags_index`.`producthashtag_id` IN('42','41','6')
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
What's wrong with this query, im selecting data from 3 different tables here. First title of exam from "class_exams" table , second selecting sum of total marks from "results" table. Query works fine without where clause.
SELECT id, exam_date , (
SELECT title
FROM class_exams
WHERE result_heads.exam_id = class_exams.id
) AS exam_title, (
SELECT sum( marks )
FROM results
WHERE result_heads.id = results.head_id
) AS obt_marks
FROM `result_heads` WHERE exam_title = 'test';
Error comes
Unknown column 'exam_title' in 'where clause'
Consider using Join
If I understand the table schema , it should be like this :
SELECT result_heads.id, result_heads.exam_date , sum( results.marks )AS obt_marks
FROM results JOIN result_heads
ON results.exam_id = result_heads.id
GROUP BY result_heads.id, result_heads.exam_date
I think you need to add the name of table in where clouse
WHERE `tbl_name`.`exam_title = 'test';
I know this is an old post, but I like to fill the answers where old posts end to prevent dead end posting. It looks like the table name is not being called out in the From clause
See this link http://www.techonthenet.com/mysql/where.php and look at the example - Joining Tables.
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