I have two tables, Table1 contains a list of records with columns super_id, user_name and job_type
Table2 contains a 3 columns as well super_id, view and time
Using a select query with criteria on table I would like to create one record per super_id in Table2
Meaning if the select query was SELECT super_id FROM Table1 WHERE job_type = “Instructor”
RF34323 through RF34328 would appear would each be inserted once into Table2 where the View column is always View1 and time is the current date.
How can an Select Insert query like this written?
The following is an example of the 2 tables:
Is this what you want?
insert into table2 (super_id, `view`, `time`)
select super_id, 'view1', now()
from table1
where job_type = 'Instructor';
Note that view and time are very BAD choices for column names, because they are keywords in SQL.
If you want to create table2, then use create table table2 as instead of insert.
Also, you can default the time column to the insertion time, if you set up the table properly.
Related
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
Im trying to use insert and select in one query.Both the tables have same number of columns except for one column in the table where data is to be inserted
table2 is the mirror image of table1 except for a column called comments;
insert into table2 select * from table1 where city='XYZ' and name = 'STY'
since the no of columns arent equal i get the following error:
Column count doesn't match value count at row 1
INSERT INTO table2 (Coln1,Coln2,Coln3,....) SELECT * FROM table1 WHERE `city`='XYZ' AND `name`='STY';
For the extra column in the table2, NULL is set by default.
I have a table Table1 .For more [details]
I am trying to do a couple of things :
- Get table T2 which have the same fields
- T2 contains rows from Table1 on responding on the following conditions :
- If the difference between the start date and end date for two different rows and for the same id is less than 3 so end date gets the value of the start date else this row will be inserted in T2
I tried to implement it
CREATE TEMPORARY TABLE tempabsences LIKE absences;
INSERT INTO tempabsences(id ,utilisateurs_id,date_debut,tempabsences.date_fin,type,statut)
SELECT absences.utilisateurs_id ,absences.utilisateurs_id,absences.date_debut,absences.date_fin,absences.type,absences.statut
FROM absences
ORDER BY absences.date_debut ASC, absences.utilisateurs_id ASC
ON DUPLICATE KEY UPDATE tempabsences.date_fin=IF(abs(DATEDIFF(tempabsences.date_fin,absences.date_debut)<=3) ,absences.date_fin,tempabsences.date_fin),
tempabsences.date_debut=IF(abs(DATEDIFF(tempabsences.date_fin,absences.date_debut)<=3) ,tempabsences.date_debut,absences.date_debut),
tempabsences.utilisateurs_id=absences.utilisateurs_id ,
tempabsences.id=IF(abs(DATEDIFF(tempabsences.date_fin,absences.date_debut)<=3),tempabsences.id,(select MAX(absences.utilisateurs_id)from absences )+absences.id )
The query don't respond on conditions , it update the row and don't insert it .
Trying to merge two game servers' tables that have this structure:
map | authid | name | time | date | ...
I would like to replace a row only if the time value of table2 is less than that of table1 AND ONLY if the map and authid values are BOTH the same. If the time values in table2 are greater, then the row from the current table (table1) should be kept untouched. Otherwise (on different map or authid values), the row from table2 should simply be appended to table1.
My way would be to
1st: create a view for replacing rows and another with the correct result set that should be appended like
--view for update
create view ChangeTable1
as
select table2.map, table2.authid,table2.name, table2.date, table1.map as t1map, table1.authid as t1authid...from table1 to inner join table2 on table1.map=table2.map and table1.athid=table2.autid
where table1.time>table2.time
-- view for appending
create view Add2Table1
select table2.map, table2.authid,table2.name, table2.date... from table2 where concat(table2.map, table2.authid) not in (select concat(table1.map, table1.authid) from table1)
-- update statement based on first view ChangeTable1
update ChangeTable1 set t1date=date, t1somevalue=somevalue......
-- insert Statement based an second view Add2Table1
insert into table 1 (map, authid, name, time, date, .... as select map, authid, name, time, date,... from Add2Table1
I hope this helps. I mostly do MS SQL, so there might be some syntax issues that need translation to MYSQL, Nils
If you need a permanent process doing this, you might consider putting this in a stored procedure
I have s MySQL database and I need to insert some specific data in a table. The data should be as follows:
SELECT id FROM a_table WHERE ... returns me a list of ids.
I need to insert n rows in second_table where n is the count of the returned rows from the first query. The second table requires 2 fields - The first one will be a record from the first query and the second one will be an integer, that I will pass from my script.
For example: If the first query returns (12,14,17,18) and the integer from my script is 5 I need to create a query, that will insert (12,5),(14,5),(17,5),(18,5) and I need this done in the database layer - I don't want to create a select statement, then create a query and then run it.
I need something like this (this is not a real query - It just shows what I need):
INSERT INTO second_table (user_id,group_id) VALUES ((12,14,17,18),5)
or to be more precise like this:
INSERT INTO second_table (user_id,group_id) VALUES ((SELECT id FROM a_table WHERE ...),5)
Is there a way to do this in SQL only (no tsql - sql only)
You can include a literal value in a SELECT:
INSERT INTO second_table (user_id, group_id)
SELECT id, 5
FROM a_table
WHERE ...
INSERT INTO
second_table
(
user_id
,group_id
)
SELECT
id
,5
FROM
first_table
WHERE
...
see the MySQL docs for more details on INSERT...SELECT syntax:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
Hi you can try query given below
Insert into items select item_sold_qty , 5 from sales
INSERT INTO second_table
SELECT id , 5 FROM a_table WHERE ...
thanks