It's a sample to see the result.
Hello Everyone,
I wrote a query as a sample and I used several selects because I needed to add columns separately.
So, I got the result.
now I want to know if can I use an update statement for this query?
select T3.*,ao_00b950_aoproject_value.VALUE ODP from
(select T2.*,ao_00b950_aoproject_value.VALUE SOD from
(select T1.*,ao_00b950_aoproject_value.VALUE Platform from (SELECT project.LEAD,project.id,project.pname,project.pkey,ao_00b950_aoproject_value.VALUE Status FROM jira820db.project
LEFT JOIN jira820db.ao_00b950_aoproject_value
on project.ID=ao_00b950_aoproject_value.PROJECT_ID
and ao_00b950_aoproject_value.FIELD_ID =1) as T1
left join ao_00b950_aoproject_value
on T1.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=26) as T2
left join ao_00b950_aoproject_value
on T2.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=28) as T3
left join ao_00b950_aoproject_value
on T3.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=27
So, I wrote this update statement:
update
(select T3.*,ao_00b950_aoproject_value.VALUE ODP from
(select T2.*,ao_00b950_aoproject_value.VALUE SOD from
(select T1.*,ao_00b950_aoproject_value.VALUE Platform from (SELECT project.LEAD,project.id,project.pname,project.pkey,ao_00b950_aoproject_value.VALUE Status FROM jira820db.project
LEFT JOIN jira820db.ao_00b950_aoproject_value
on project.ID=ao_00b950_aoproject_value.PROJECT_ID
and ao_00b950_aoproject_value.FIELD_ID =1) as T1
left join ao_00b950_aoproject_value
on T1.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=26) as T2
left join ao_00b950_aoproject_value
on T2.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=28) as T3
left join ao_00b950_aoproject_value
on T3.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=27) as ww
set project.LEAD = 'kami'
where project.ID ='10000';
But unfortunately, it's not working and I'm not surprised because I'm not an expert in MySQL.
Does anybody have any suggestions?
Or I'm writing wrong the code like always.
With the first query, I get my result, and run perfectly.
With the second one, I get an error code 1288 "The target table ww of the UPDATE is not updatable".
Related
My brain is starting to hurt with this query and I would appreciate some guidance. What I am trying to get as the result of this query are three values: attendanceScore, lootScore, and totalScore (attendanceScore - lootScore).
Attendance is tracked across three tables: attendance, attendancelog, and attendancevalues.
attendance records an individual's attendance status attached to an attendancelog record. There are types of attendance such as "attended", "missed", and "called out".
attendancelog is the parent record which records the event type, title and date as well as who logged the attendance and when.
attendancevalues is a config table that matches the attendance type from attendance and the event type from attendancelog and returns a configurable value FLOAT.
Loot is tracked across two tables: loot and loottypes.
loot logs each individual item, who received it and when and what type of loot it was (Primary, Secondary, Free-for-all).
loottypes is a config table that takes the type from loot and returns a configurable cost FLOAT.
After some work I have come up with a working query to get attendanceScore and lootScore:
SELECT
(SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3) as attendanceScore,
(SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2
ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3) as lootScore
I know this doesn't work, but I tried to add (attendanceScore - lootScore) to the query but it says those fields are not available. That is ultimately what I need to complete the query.
I can get the result I want by copying each of the subqueries directly into (attendanceScore - lootScore) but it is just absolutely hideous and I'm sure unnecessary:
SELECT
(SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3) as attendanceScore,
(SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2
ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3) as lootScore,
(
(SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3) - (SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2
ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3)
) as totalScore
Could someone help me understand what methods to use for cleaning this up into something more streamlined and efficient?
You may use an inline view
SELECT attendanceScore,
lootScore,
attendanceScore - lootScore as totalScore
FROM
(
SELECT
(
SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3
) as attendanceScore,
(
SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2 ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3) as lootScore
) t
I'm trying to update a table in MySQL with data from another table.
UPDATE KassaticketRegels
SET soort = (SELECT t3.benaming
FROM KassaticketRegels AS t1 INNER JOIN Diensten AS t2 ON t1.dienst = t2.id INNER JOIN DienstGroepen AS t3 ON t2.dienstGroep = t3.id
WHERE t1.id = KassaticketRegels.id)
When i simulate the query, it gives me 304 matched rows.
But when i press go, i get the error "#1093 - Table 'KassaticketRegels' is specified twice, both as a target for 'UPDATE' and as a separate source for data".
How can i solve this?
Looking to your code seems you need an update on inner join
UPDATE KassaticketRegels t1
INNER JOIN Diensten AS t2 ON t1.dienst = t2.id
INNER JOIN DienstGroepen AS t3 ON t2.dienstGroep = t3.id
set t1.soort = t3.benaming
Hi I have the following working query, but I'm looking to move away from the sub query approach.
select t0.emp_id, t1.first_name, t1.last_name
from employee_profile As t0 left join user_profile as t1 on t0.user_profile_id = t1.id
where t0.emp_id not in (select t4.emp_id from time_sheet as t4 where t4.ts_end_date = '2014-10-08')
There is 1301 employee profiles
There is 337 that do not have time_sheets for end date 2014-10-08
This works perfectly thus far.
Now I'm trying to migrate away from the sub query and into a join. I've tried the following without success.
With this query, I get the complete opposite of what I'm looking for which is 964 results.
select t0.emp_id, t1.first_name, t1.last_name, t3.emp_id
from employee_profile As t0 left join user_profile as t1 on t0.user_profile_id = t1.id
left join time_sheet as t3 on t0.id = t3.employee_profile_id
I've also tried the following using where conditions and got 0 results
1.
where t3.ts_end_date = '2014-10-08'
and t3.employee_profile_id is null
2.
where t3.ts_end_date = '2014-10-08'
and t3.id is null
Does anybody know how to fix this?
Figured it out
select t0.emp_id, t1.first_name, t1.last_name, t3.emp_id
from employee_profile As t0 left join user_profile as t1 on t0.user_profile_id = t1.id
left join time_sheet as t3 on t0.id = t3.employee_profile_id and t3.ts_end_date = '2014-10-08'
where t3.employee_profile_id is null
Suppose I have following tables: T1,T2 and T3.
How could I rephrase the following query using only left joins.
Select *
From T1
Right join T2 On T1.FK2=T2.PK
Right join T3 On T1.FK3=T3.PK
Following attempt is not correct:
Select *
From T2
Left join T1 On T1.FK2=T2.PK
Left join T3 On T1.FK3=T3.PK
T3 is On the wrong Side of the join. Is the following possible:
Select *
From T2
Left join T3 On T1.FK3=T3.PK
Left join T1 On T1.FK2=T2.PK
I can't Find a way to put both tables 2 and 3 On the left Side of 1 and use the correspondent fields to join all tables? The last query uses fields of table 1 before this table is mentioned in the query.
Or something like this?
Select *
From T2
Left join (
T3 left join T1
On T1.FK3=T3.PK)
On T1.FK2=T2.PK
Apparently brackets can help to order your joins. I wonder if this is really documented, i've found Nothing at first glance in the mysql docs.
Following query is correct and does not have any subqueries:
Select T1.Id Ida, t2.id idb, T3.id idc FROM T3
LEFT JOIN
(T2
LEFT JOIN T1 ON (T1.ID = T2.ID))
ON (T1.ID= T3.ID);
You need to use a subquery to first join t1 with t2 and then join the result with t3:
SELECT T.ID1 ID1, T.ID2 ID2, T3.ID ID3 FROM T3
LEFT JOIN
(SELECT T1.ID ID1, T2.ID ID2 FROM T2
LEFT JOIN T1 ON (T1.ID = T2.ID)) T
ON (T.ID1 = T3.ID);
SQL Fiddle
The first way is just to reverse the order that the tables are mentioned:
Select *
from t3 left outer join
t2
on T1.FK3 = T3.PK left outer join
t1
on T1.FK2 = T2.PK
But this won't work, because the first condition is on t1 and not t2. And t2 hasn't yet been defined.
When working with chains of tables in left or right outer joins, only the first (or last) tables are important, because they "drive" the query. "Drive" in the sense that they provide all the values even when there are no matches. So, the following should do what you want:
Select *
from t3 left outer join
t1
on T1.FK3 = T3.PK left outer join
t2
on T1.FK2 = T2.PK;
I have a mysql statement below
select c11 as genre, t3.strGenre, t2.idGenre t2idGenre, t1.idMVideo
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock'
This works fine and gives me the desired results.
Now when I want to put this into an update statement i get stuck.
update t2
set idGenre=62
where (select c11, t3.strGenre, t2.idGenre t2idGenre
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock')
I know this is not correct. Any help would be appreciated.
Try:
update genrelinkmusicvideo t2
join musicvideo t1
on t2.idMVideo = t1.idMVideo
join genre t3
on t3.idGenre = t2.idGenre
set t2.idGenre = 62
where c11 <> strGenre and genre = 'Alternative Rock'
FYI, I would recommend using table_alias.column (e.g. t3.genre = 'Alternative Rock'), since it makes thing much more clear, particularly for those who do not know the schema already.
You could try:
update genrelinkmusicvideo
set idGenre=62
where idGenre IN (select t2.idGenre
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock')