I have table "nol_voa" with different values and I am importing xml file with values in that table I want to insert in another table "#tmpRcIzm" the "id" values where the field "C_REF" has changed its value.
This is the code, what I wrote, but there is a mistake, it always adds two more "id" values which have not been changed.
insert into #tmpRcIzm
select distinct
a.id
from
openxml(#hDoc, '/art_komplekts/nol_voa') with #vc xd
join nol_art a on xd.art_cd = a.cd
left join #tmp t on t.cd = xd.art_cd
inner join nol_voa v on xd.id_art = v.id_art
where
xd.C_REF!=v.C_REF
You left join of #tmp, can introduce duplicates, and also the join on nol_art serve no purpose on this SQL. remove those two, and you should elminate your dups.
Related
https://imgur.com/a/0443EiL
Above is the image of what I am working with.
I need to insert the Styletype column so that the StyleYype matches up with the StyleCode column.
seems you need update based on join
update table1
inner join table2 ON table2.style = table1.styleCode
set table1.styleType = table2,styleType
The aim of my query is to use a value from one table A to look up a corresponding value from another table B, and then use that value to look up another value from another table C. This value should then be used to update a column in the original table A.
In my query so far I have managed to get a correct list of the values from table C using this command. In this command, Disbursements, Employee_Details & List_State_Codes correspond to tables A, B & C respectively as described above.
SELECT `List_State_Codes`.`Code`
FROM `List_State_Codes`
LEFT JOIN (
`Employee_Details` , `Disbursements`
) ON ( `Employee_Details`.`STATE` = `List_State_Codes`.`STATE` )
WHERE `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
This returns the correct list of values as required: One value from table C for each row in table A. Now my issue is to update the required column from table A with these returned values. This is where I am stuck.
The following query is what I believe to be my closest attempt:
UPDATE `Disbursements`
SET `Disbursements`.`CostCentreID` =
(
SELECT `List_State_Codes`.`Code`
FROM (SELECT * FROM `List_State_Codes`) AS `table`
LEFT JOIN (
`Employee_Details` , `Disbursements`
) ON ( `Employee_Details`.`STATE` = `List_State_Codes`.`STATE` )
WHERE `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
)
I receive the
error #1093 - You can't specify target table 'Disbursements' for update in FROM clause, despite adding the FROM (SELECT * FROM List_State_Codes) AS table line.
Thanks for any help.
You can join in the update, and this can help to remove the offending table from the subquery:
UPDATE `Disbursements`
JOIN `Employee_Details`
ON `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
LEFT JOIN `List_State_Codes`
ON `Employee_Details`.`STATE` = `List_State_Codes`.`STATE`
SET `Disbursements`.`CostCentreID` = `List_State_Codes`.`Code`
However, I could not test this, so let me know if this helps. (or if some other error occurs)
Thanks to Paul's help I have found the correct solution to my problem. The second join was what I needed as Paul suggested, along with an additional table alias to prevent the classic #Error 1903. I also needed to add the List_State_Codes (table C) to the JOIN to ensure it could access the correct data.
UPDATE `Disbursements`
JOIN `Employee_Details`
ON `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
LEFT JOIN (
`Employee_Details` d,
`List_State_Codes`
) ON
`Employee_Details`.`STATE` = `List_State_Codes`.`STATE`
SET `Disbursements`.`CostCentreID` = `List_State_Codes`.`Code`
Is there a way to select a set of values from one table and then use those values in an IN clause?
I want to select IDs from one table and then update data for those IDs in another table.
So something like
<some var> = SELECT id from tableA WHERE <something>;
INSERT INTO tableB <stuff> where id IN (<some var>);
I release the variable syntax isn't real. just want to display my intent. I have read about SET a little but am still new to MySQL so it doesnt make perfect sense. Also it mentioned that SET could only set variables of certain types and they all seemed to simple.
Thanks!
You can use insert . . . select:
INSERT INTO tableB (id)
SELECT id
FROM tableA
WHERE <something>;
I'm not sure what IN has to do with this.
EDIT:
Oh, you want an update:
update tableb b join
tablea a
on b.id = a.id
set b.col = ??
where <conditions on a>;
You can also do this using in:
update tableb b
set col = ??
where b.id in (select a.id from tablea a where <conditions>);
The biggest difference is whether or not you want to use information from tablea. If you do, then you need the join version.
What I am trying to do is to insert records from multiple tables into 1 table by joining. Here is what I have and I can't seem to get it to work. I get the following error #1136 - Column count doesn't match value count at row 1
INSERT INTO users(`name`, `email`,`location_id`, `department_id`)
VALUES('John Doe', 'jdoe#email.com', 1, 1)
INSERT INTO extensions(`ext`)
VALUES(98765)
INSERT INTO dids(`did`)
VALUES('1-800-555-5555')
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT extensions.*, dids.*, users.*
FROM extensions tbl_ext, dids tbl_dids, users tbl_users, users_numbers usn
Inner Join users ON users.id=usn.user_id
Inner Join extensions ON extensions.id=usn.ext_id
Inner Join dids ON dids.id=usn.did_id
WHERE tbl_users.name = 'John Doe'
AND tbl_users.email = 'jdoe#email.com'
AND tbl_ext.ext = 98765
AND tbl_dids.did='401-559-9999';
The first 3 insert statements work. The error fires on the 4 insert when I try to join. Can anyone help.
Use LAST_INSERT_ID():
INSERT INTO users(`name`, `email`,`location_id`, `department_id`)
VALUES('John Doe', 'jdoe#email.com', 1, 1);
SET #user_id = LAST_INSERT_ID();
INSERT INTO extensions(`ext`)
VALUES(98765);
SET #ext_id = LAST_INSERT_ID();
INSERT INTO dids(`did`)
VALUES('1-800-555-5555');
SET #did_id = LAST_INSERT_ID();
INSERT INTO users_numbers(user_id,ext_id,did_id)
VALUES (#user_id,#ext_id,#did_id);
Instead of using user variables (#user_id,#ext_id,#did_id) you can also fetch the LAST_INSERT_ID in your application language.
There are two problems.
First, the SELECT list has to return only the columns that you want to insert into the table. The error is because you're returning tablename.* rather than specific columns.
Second, the SELECT query is very wrong. You have the tables that you're joining listed twice: first in the FROM clause and then again in the INNER JOIN clauses. You should only list them once (unless you're doing a self-JOIN, which is not needed here).
And you shouldn't be joining with users_numbers -- that's the table you're trying to add to, so it doesn't have the IDs for these rows yet.
What you want is a simple cross join between the rows from each of the source tables that match the values you're combining into the relation table.
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT u.id, e.id, d.id
FROM users AS u
CROSS JOIN extensions AS e
CROSS JOIN dids AS d
WHERE u.name = 'John Doe'
AND u.email = 'jdoe#email.com'
AND e.ext = 98765
AND d.did='401-559-9999';
You need to be very specific about which columns you want from that SELECT. Right now you're grabbing everything from the three tables, that's too much.
Maybe you mean:
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT users.id, extensions.id, dids.id
FROM ...
When composing complicated queries like this, run the SELECT subcomponent separately to ensure it produces the right data. I bet if you ran your version it'd show a bunch of columns.
I have tree table
a (a_col1, a_col12, a_col3)
b (b_col1, b_col12, b_col3)
c (c_col1, c_col12, c_col3)
I want to write the b.b_col3 to c.c_col3
where a.a_col1 equals to b.b_col12.
What am I doing wrong ?
INSERT INTO c(c_col3)
SELECT a.a_col1, b.b_col12
FROM a LEFT JOIN b
ON
a.a_col1 = b.b_col12;
You are trying to insert 2 columns value in single column, use something like below-
INSERT INTO c(c_col2,c_col3) SELECT a.a_col1, b.b_col12 FROM a LEFT JOIN b ON a.a_col1 = b.b_col12;
You can not do both stuff with one query. You cannot INSERT and SELECT at the same time. Try first selecting and then inserting if it is possible.