mySQL INSERT query with condition - mysql

I have two table: employee and privremeno and the both of them contains column jmbg. I want insert in employee (two columns) data from privremeno (two columns) so that data would be inserted in row where jmbg in employee is equal (the same) to jmbg in privremeno.
Something like:
INSERT INTO vg_pka.employee (stazDani, godZivota) select ukstaz, gz from
vg_pka.privremeno where vg_pka.privremeno.jmbgl = vg_pka.employee.jmbg;
How to do that?

you have to use update to inser values to existing table . I cant get your correct requirement . But the below is the syntax you have to use.
update table_name set = 'value' where (your condition)

You can use a join:
INSERT INTO
vg_pka.employee (stazDani, godZivota)
SELECT
ukstaz, gz
FROM
vg_pka.privremeno p
INNER JOIN
vg_pka.employee e
ON
p.jmbgl = e.jmbg;

Related

How to match and insert new table column to a previous database column

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

Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement

I am trying to get a minimum value from the Candidate table and insert that value in the MinTotal table. Can you do both in one SQL statement?
Here's my SQL Statement:
UPDATE MinTotal SET MinTotal.min_total= MIN(CandidateID.TotalVotes);
UPDATE MinTotal a
INNER JOIN (SELECT MIN(c.TotalVotes) min_vote, c.CandidateID FROM Candidate c
GROUP BY c.CandidateID) b ON b.CandidateID = a.CandidateID
SET a.min_total = b.min_vote;
Try the above. This is specific for each candidate, else you can use the other answers provided.
You have to use a select so you can properly set your MIN().
One way of doing that would be like that:
UPDATE MinTotal
SET
min_total = Cmin.minresult
FROM (
SELECT MIN(TotalVotes) as minresult
from CandidateID
) Cmin
In general, that would be one way to solve the Problem. In this case you would set the minresult for every row you have in your MinTotal table. If you dont want that, you may need to be more specific about your desired output and add some examples in your question
UPDATE MinTotal
SET MinTotal.min_total = (
SELECT MIN(TotalVotes)
FROM CandidateID
);

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.

Replace column values in mySQL table with values from a second table

I have a table named "ONE" with a column "Names".
I have a second table called "TWO" with two columns : "Old_Names" and "New_Names".
I want to replace the "Names" in table "ONE" with the "Old_Names" from table "TWO".
The values in "Names" are the same as in "Old_Names" in my tables.
I am trying to do that but I get an error on mySQL :
update ONE set (ONE.Names=TWO.New_Names)
from ONE
join TWO on (ONE.Names=TWO.Old_Names);
Look at the update query :
update one o
join two t on ( o.names = t.old_names )
set O.names = t.new_names;
The join clause has to be write at the beginning of the query =>
http://sqlfiddle.com/#!9/9f292c
update ONE
join TWO on ONE.Names = TWO.Old_Names
set ONE.Names = TWO.New_Names

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)
)