I am trying to create change a single row query into a multiple-row query.
I need to grab all id numbers that equal "286" from table_A table and grab all student numbers from the table_B table that have a created-at date greater or equal to "2021-08-25 00:00". These values will be inserted into table_main.X and table_main.Y columns respectively.
Here is the code I was given to work with:
INSERT INTO `table_main`(`X`,`Y`)
SELECT table_A.id,
table_B.student
FROM `table_A`
INNER JOIN `table_B`
WHERE table_A.id = '286'
AND table_B.created_at >= '2021-08-25 00:00'
GROUP BY `student`.
I am getting the following error:
-- duplicate entry '286-111111' for key table_main.PRIMARY --
Is there a way to condense all the duplicates as one when they get entered?
Related
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.
I have four tables [tbl1], [tbl2], [tbl3], [tbl4]. The first three tables have records of their own, from which a selected few is to be inserted into the fourth table i.e [tbl4]. I'm expected to use a stored procedure whose parameter specifies the number of records to be inserted into [tbl4]. For example:
CALL storedproc(4);
will insert 4 records into tbl4 according to the inner join statement and seeing their common id's.
But mysql keeps giving error: "Duplicate entry 'some_name' for key 'PRIMARY' ".
BEGIN
DECLARE nums int(50);
SET nums=0;
INSERT INTO tblstudteach (SELECT tblstudent.id, tblstudent.Namess, tblgrades.Mark, tblgraderange.Score, tblteacher.teachername FROM tblstudent INNER JOIN tblgrades ON tblstudent.id=tblgrades.stdid
INNER JOIN tblgraderange ON tblgrades.Mark BETWEEN tblgraderange.StartingGrade
AND tblgraderange.EndingGrade INNER JOIN tblteacher ON tblstudent.id=tblteacher.studid)
LIMIT num;
SET nums=nums+1;
END
I also have no clue as how I could include a loop statement that can loop through each record and insert into [tbl4].
i had query like this
CREATE TRIGGER `tambah_riwayatobat` AFTER INSERT ON `obat`
FOR EACH ROW insert into riwayat_obat(nama, keterangan, distributor,tanggal)
(select new.nama, 'Masuk', d.nama ,now()
From distributor d
join obat ON new.id_distributor = d.id_distributor)
i try to insert data with trigger and one of part data i fetch with constraint, but why the data be duplicate entry ?
Output :
example, if i try to insert data obat 1st time, data on tambah_riwayatobat insert 1 too
if i try to insert data obat 2nd time, data on tambah_riwayatobat insert 2 times with same data
if i try to insert data obat 3rd time, data on tambah_riwayatobat insert 3 times with same data
I'm not certain exactly what's happening, but it's a result of the join in your trigger code. You’re joining obat to distributor, but your join condition makes no mention of obat so you're getting some sort of cross-product where on the second and subsequent INSERT your SELECT subquery is selecting more than one row.
You shouldn't (and don't need to) use the join, since all the data you need from obat is already in the pseudorecord NEW. The following code should work much better:
CREATE TRIGGER `tambah_riwayatobat`
AFTER INSERT ON `obat`
FOR EACH ROW
INSERT INTO riwayat_obat
(nama, keterangan, distributor, tanggal)
(SELECT NEW.nama, 'Masuk', d.nama, now()
FROM distributor d
WHERE new.id_distributor = d.id_distributor
LIMIT 1);
The LIMIT clause will ensure that the SELECT selects only one row, so the INSERT inserts only one row; if distributor.id_distributor is a primary key the LIMIT clause is unnecessary.
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 .
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