Selecting and storing values, using those values in IN clause - mysql

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.

Related

MySQL JOIN INSERT

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.

mySQL update a value

Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)

mysql 3 table join with update

I am trying to update the status of a column, by checking two joined tables. Even though I dont get an error. The colum is not updating. I want to take the general blockplot id and see if there is a transaction that matches and or a a container. If there is a transaction but no container i need to mark it as P.
UPDATE (general
LEFT JOIN
transactions
ON
general.blockplotid=transactions.blockplotid)
LEFT JOIN
container
ON
general.blockplotid=container.blockplotid
SET general.lotstatus = 'P'
WHERE general.lotstatus != 'U' AND
transactions.id_transaction IS NOT NULL AND
container.id_container IS NULL
So summarize, I have 3 tables. I only want to update one colum in one table. I want to check for values in the other two tables, their values depend upon the set value. The three tables are connected with a primary key to foreign key.
When I do a double join select statement. The query seems correct.
SELECT transactions.blockplotid AS blockplotid_2, container.blockplotid AS blockplotid_1, general.blockplotid, general.lotstatus, container.id_container, transactions.id_transaction
FROM ((general LEFT JOIN transactions ON general.blockplotid=transactions.blockplotid) LEFT JOIN container ON general.blockplotid=container.blockplotid)
ORDER BY general.blockplotid ASC
However it seems as though the join for the update isnt like the select.
This query seemed to work:
This query worked:
UPDATE ((general LEFT JOIN transactions ON transactions.blockplotid=general.blockplotid) LEFT JOIN container ON container.blockplotid=general.blockplotid)
SET general.lotstatus='P'
WHERE general.blockplotid!='U' AND container.id_container is null AND transactions.id_transaction is not null
The difference here is the case of IS NOT NULL and also the order of the where condition.
Is there any explanation for this?
I suspect the parentheses/locations of the joins. Try a setup like this:
UPDATE
Table1
SET
Table1
. Field1 = StagingTable . Field1
FROM
Table1
INNER JOIN StagingTable
ON Table1 . Field2 = StagingTable . Field2
WHERE
StagingTable . Field3 IS NOT NULL
I would suggest restructuring your UPDATE statement to have an embedded SUB-SELECT. update general set lotstatus = (SELECT .... WHERE .... )

Issue with the TOP 1 query

Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1

sql query for deleting rows with NOT IN using 2 columns

I have a table with a composite key composed of 2 columns, say Name and ID. I have some service that gets me the keys (name, id combination) of the rows to keep, the rest i need to delete. If it was with only 1 row , I could use
delete from table_name where name not in (list_of_valid_names)
but how do I make the query so that I can say something like
name not in (valid_names) and id not in(valid_ids)
// this wont work since they separately dont identity a unique record or will it?
Use mysql's special "multiple value" in syntax:
delete from table_name
where (name, id) not in (select name, id from some_table where some_condition);
If your list is a literal list, you can still use this approach:
delete from table_name
where (name, id) not in (select 'john', 1 union select 'sally', 2);
Actually, no I retract my comment about needing special juice or being stuck with (AND OR'ing all your options).
Since you have a list of values of what you want to retain, dump that into a temporary table. Then do a delete against the base table for what does not exist in the temporary table (left outer join). I suck at mysql syntax or I'd cobble together your query. Psuedocode is approximate
DELETE
B
FROM
BASE B
LEFT OUTER JOIN
#RETAIN R
ON R.key1 = B.key1
AND R.key2 = B.key
WHERE
R.key1 IS NULL
The NOT EXISTS version:
DELETE
b
FROM
BaseTable b
WHERE
NOT EXISTS
( SELECT
*
FROM
RetainTable r
WHERE
(r.key1, r.key2) = (b.key1, b.key2)
)