I wrote a query, and Im sure that it was right form.
But I get an error. :)
May I do this query else?
UPDATE pages SET
p_name = 'Activites',
p_active = 1,
p_parent = 'sport',
p_parent_id=(
SELECT p_id FROM pages WHERE p_link='sport' LIMIT 1
),
p_link = 'activites'
WHERE p_id = 9;
Thank you.
You can't do that this way. You need to cross join the table and subquery:
UPDATE
pages AS p
CROSS JOIN (
SELECT p_id FROM pages WHERE p_link='sport' LIMIT 1
) AS sq
SET
p.p_name = 'Activites',
p.p_active = 1,
p.p_parent = 'sport',
p.p.parent_id= sq.p_id
p.p_link = 'activites'
WHERE p.p_id = 9;
Related
Problem 1:
I need to add a field in a WHERE clause with a condition in my stored procedure, so what is the right syntax for the below query in CASE condition?
Problem 2:
OR condition is giving me all records with values 0,1,2,3,4 for below condition (I need tqm.is_imp = 1 if not found then tqm.is_imp IN (0,2,3,4)). What should I do? Please help.
tqm.is_imp = 1 OR tqm.is_imp IN (0,2,3,4)
My Query:
SELECT tqm.id INTO selectedQuestionNumber
FROM tc_question_master tqm
INNER JOIN tc_question_mapping tqmap ON tqmap.tc_question_id = tqm.id
WHERE tqmap.syllabus_chapter_details_id IN (SELECT cms_id from topic where chapter_id IN (SELECT id from MyChapters))
AND tqm.marks = CurMark
CASE WHEN CurQuestionType > 0 THEN
AND tqm.question_type = CurQuestionType
END
AND (tqm.level_of_question = 'Easy' OR tqm.level_of_question
IN ('Moderate','Difficult','Very difficult'))
AND (tqm.is_imp = 1 OR tqm.is_imp IN (0,2,3,4))
AND tqm.id NOT IN (SELECT q.question_id FROM QBDetails q)
ORDER BY RAND() LIMIT 1;
The upper part is just a reformat, but lower as you approach the logic that was a case expression, most of that is pure guess.
SELECT
tqm.id INTO selectedQuestionNumber
FROM tc_question_master tqm
INNER JOIN tc_question_mapping tqmap ON tqmap.tc_question_id = tqm.id
WHERE tqmap.syllabus_chapter_details_id IN (
SELECT
cms_id
FROM topic
WHERE chapter_id IN (
SELECT
id
FROM MyChapters
)
)
AND (
tqm.level_of_question = 'Easy'
OR tqm.level_of_question IN ('Moderate', 'Difficult', 'Very difficult')
)
AND (
tqm.is_imp = 1
OR tqm.is_imp IN (0, 2, 3, 4)
)
AND tqm.id NOT IN (
SELECT
q.question_id
FROM QBDetails q
)
## now it is really vague
AND (
tqm.marks = CurMark
OR
(CurQuestionType > 0 AND tqm.question_type = CurQuestionType)
)
## end of guesswork
ORDER BY
RAND()
LIMIT 1;
I would want to count entries in a column and display the count beside it.
However, I'm clueless on how can I do it.
Desired output:
arrangement_number tray_no rl_type flag(count of occurrence)
------------------ ------- ---- ----
2774818 381001 R 3
2774818 381001 R 3
2774818 381001 L 3
2778470 405128 R 1
2779702 265265 R 2
2779702 265265 R 2
I'm currently trying queries using #variables but I still cant get it right.
each row are unique and I need them not to be grouped.
Update: Expanded Table added source code
Note: I'm currently joining 5 tables now
Actual Query:
SELECT
log.arrangement_number,
header.tray_number,
detail.rl_type,
-- some more fields here
FROM
log
INNER JOIN
header ON log.arrangement_number = header.rxarrangement_number
AND log.production_place_code = header.production_place_code
INNER JOIN
detail ON log.arrangement_number = detail.rxarrangement_number
AND log.production_place_code = detail.production_place_code
INNER JOIN
deliveryperiod ON log.arrangement_number = deliveryperiod.arrangement_number
AND log.production_place_code = deliveryperiod.production_place_code
AND detail.rl_type = deliveryperiod.rl_type
INNER JOIN
calc ON calc.arrangement_number = log.arrangement_number
AND calc.production_place_code = log.production_place_code
AND deliveryperiod.rl_type = calc.rl_type
AND detail.rl_type = calc.rl_type
WHERE
header.status_code IN ('20' , '21')
AND log.process_code = '12'
AND deliveryperiod.process_code_current = '12'
AND deliveryperiod.sub_process_code_current IN ('100' , '105')
AND lot_number = '120131'
ORDER BY log.lot_number , log.sequence_number , deliveryperiod.rl_type DESC
SELECT t1.tray_no,
t2.flag
FROM yourTable
INNER JOIN
(
SELECT tray_no, COUNT(*) AS flag
FROM yourTable
GROUP BY tray_no
) t2
ON t1.tray_no = t2.tray_no
try this...
SELECT tray_no, COUNT(*) 'flag'
FROM table1
GROUP BY tray_no
I have this statement that works fine without the below statement.
I think I may be using the incorrect statement. What I'm trying to do is only select the first productsapplied.applicationid. If another row has the same productsapplied.applicationid as one already selected it won't select it. There can be more than one of the same application id but I need it to only add 1.
DISTINCT('productsapplied'.applicationid)
'SELECT `productsApplied`.id, DISTINCT(`productsApplied`.applicationid)
FROM `productsapplied`
INNER JOIN `products`
ON `productsApplied`.productid = `products`.id
INNER JOIN `applications`
ON `productsApplied`.applicationid = `applications`.id
WHERE `applications`.clubid = ? AND `applications`.area = ? AND EXTRACT(YEAR FROM `applications`.date) = ? AND `products`.producttype = ?
If anyone has any ideas, would appreciate it!
If not I was thinking of just doing a COUNT DISTINCT
Lets say that here are the products applied
id: 3 clubid:6 applicationid: 5 ...
id: 4 clubid:6 applicationid: 5 ...
id: 5 clubid:6 applicationid: 5 ...
id: 4 clubid:6 applicationid: 6 ...
Presuming the rest of the statement holds e.g. year = ? etc..
Then the number of rows returned would be 2. AS there are 3 rows with the same application id. No matter how many rows there are with the same application id, one should be counted.
DISTINCT is applied is to whole row not to a single column for your concern there will be different productsApplied.ids per applicationid so you can grab all by using group_concat
SELECT
GROUP_CONCAT(pa.id),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
If you are not concerned with productsApplied.ids then you can simple use the group by part but note group by without aggregate function will result in indeterminate order
Edit
This will give you one max id per applicationid
SELECT
SUBSTRING_INDEX(GROUP_CONCAT(pa.id ORDER BY pa.id DESC), 1),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
or
SELECT
MAX(pa.id),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
Select min(pa.id), pa.applicationid
From productsapplied pa
Join ...
...
Group by pa.applicationid
Please could someone give me the correct syntax for below.
MySQL UPDATE tblcontact SET MainContact = 1
WHERE COUNT(tblcontact.CompanyID) = 1
GROUP BY tblcontact.CompanyID
I get the idea. You want to set the field to 1 where there is only one record. Try this:
UPDATE tblcontact c join
(select CompanyID, count(CompanyID) as cnt
from tblcontact
group by CompanyId
) cc
on c.CompanyId = cc.CompanyId and cnt = 1
SET c.MainContact = 1 ;
I have a master table called "parent" and a related table called "childs"
Now I run a query against the master table to update some values with the sum from the child table like this.
UPDATE master m SET
quantity1 = (SELECT SUM(quantity1) FROM childs c WHERE c.master_id = m.id),
quantity2 = (SELECT SUM(quantity2) FROM childs c WHERE c.master_id = m.id),
count = (SELECT COUNT(*) FROM childs c WHERE c.master_id = m.id)
WHERE master_id = 666;
Which works as expected but is not a good style because I basically make multiple SELECT querys on the same result. Is there a way to optimize that? (Making a query first and storing the values is not an option.
I tried this:
UPDATE master m SET (quantity1, quantity2, count) = (
SELECT SUM(quantity1), SUM(quantity2), COUNT(*)
FROM childs c WHERE c.master_id = m.id
) WHERE master_id = 666;
but that doesn't work.
Update: Here is the solution, thanks to everbody:
You can do something like this:
UPDATE master m
INNER JOIN childs c ON m.master_id = c.master_id
SET master.quantity1 = c.quantity1,
master.count = 1
If you have only one child record at a time. However if you want to use a group function like SUM() in the joined table that doesn't work. Either you get a "Invalid use of group function" if you leave the "group by" part or a "You have an error in your sql syntax if you use "GROUP BY c.master_id"
-- This doesnt work :(
UPDATE master m
INNER JOIN childs c ON m.master_id = c.master_id
SET master.quantity1 = SUM(c.quantity1),
master.count = COUNT(c.*)
GROUP by c.master_id
The solution is to use JOIN with a subquery:
UPDATE master m
INNER JOIN
(
SELECT master_id,
SUM(quantity1) as quantity1,
COUNT(*) as count
FROM childs c
GROUP BY master_id
) c
ON c.master_id = m.master_id
SET m.quantity1 = c.quantity1,
m.count = c.count
WHERE m.master_id = 666;
But since this pulls every row from the childtable the overhead would likely be bigger than using more subqueries like in the original sql. So you should add a WHERE clause to the joined table to get only the rows you need.
Another interesting approach is this syntax, which does the same as the JOIN with the WHERE clause but you should only use if if you want to update all rows with the same values and your subquery only returns one row, since the result from the subquery gets appended to the result and can be used like any column.
UPDATE master m,
(
SELECT SUM(c.quantity1) as sum_of_quantity,
COUNT(*) as rowcount FROM child c WHERE c.master_id = 666
) as c
SET m.quantity1 = c.sum_of_quantity,
m.count = c.rowcount
WHERE m.master_id = 666;
Rewriting Lieven's solution to MySQL:
UPDATE master m
JOIN (
SELECT master_id
, SUM(quantity1) as quantity1
, SUM(quantity2) as quantity2
, COUNT(*) as count
FROM childs c
GROUP BY
master_id
) c
ON c.master_id = m.master_id
SET
m.quantity1 = c.quantity1
,m.quantity2 = c.quantity2
,m.count = c.count
WHERE m.master_id = 666;
I don't know if it is allowed in MySQL, but SQL Server allows you to use the result of a select in an update.
UPDATE master m SET
quantity1 = c.quantity1
, quantity2 = c.quantity2
, count = c.count
FROM master m
INNER JOIN (
SELECT master_id
, quantity1 = SUM(quantity1)
, quantity2 = SUM(quantity2)
, count = COUNT(*)
FROM childs c
WHERE master_id = 666
GROUP BY
master_id
) c ON c.master_id = m.master_id
You could select your data into a temporary table, and then update using that data.
If you also want to insert "new" data in the same roundtrip, look into INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE ...
If you already are doing inserts if row doesn't exist, then that would be redundant with this example.
example:
INSERT INTO master m (id, quantity1, quantity2, count)
SELECT master_id, SUM(quantity1) q1, SUM(quantity2) q1, COUNT(*) c
FROM childs
GROUP BY master_id
ON DUPLICATE KEY UPDATE
m.quantity1 = q1,
m.quantity2 = q2,
m.count = c
NOTE! This is untested code, but I think it should be possible to backreference the select result in the UPDATE.
Syntax reference: http://dev.mysql.com/doc/refman/5.0/en/insert.html