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 .
Related
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?
I'm trying to fill a certain column of a SQL table with data from another table. I have a column named "size" in my table which should return the number of rows in the 2nd table where the id of both rows is the same. Is there a way to populate a SQL column based on a certain command? I would love to be able to fill the column based on this command:
SELECT count(*)
FROM second_table
WHERE id = "row_id";
Here is a sample database with the two tables:
Table 1
Name
id
tiger
1
lion
2
gazelle
1
Here is the desired output for Table 2:
id
Number of Animals
1
2
2
1
I am trying to fill the Number of Animals column but do it automatically and dynamically when another row is added or deleted to Table 1, which is why I want the Select count(*) SQL statement as the code for the column.
One method is a correlated subquery:
update table1 t1
set size = (select count(*)
from table2 t2
where t2.id = t1.id
);
If you need to do this dynamically (as data is inserted), then you would need to use a trigger. However, I would suggest that you calculate the value as needed, unless there is a specific reason why you need to store it.
I guess you need something like this:
CREATE TRIGGER UpdateAnimalCountTable2
AFTER INSERT ON `Table1` FOR EACH ROW
begin
DECLARE NewCount int;
SELECT count(1)
INTO #NewCount
FROM Table1
WHERE Table1.id= NEW.id;
UPDATE Table2
SET NoOfAnimals = #NewCount
WHERE id = NEW.id;
END;
Above is the trigger which will be executed after every insert in Table1 and will update the count in Table 2 for ID which just got inserted in Table1.
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.
Currently, I find it time consuming to copy specific columns and values from Table 1 and paste the values onto Table 2 manually.
I have two tables ( In the same database ) like this:
How can I save time by doing the following? -
Grab specific ID from Table 1, in this case 1 and 2.
and copy just the Pcode & Desc values onto Table 2?
This is the end result I want to achieve (screenshot below)
The ID will be new, because its a new record. So technically I am updating Table 2 with new values that I have copied from Table 1
Every column is varchar type column expect the Id's
Also, I am using MySql Workbench.
This should do the trick;
INSERT INTO table2 (PCode, Desc)
SELECT Pcode, Desc
FROM table1
WHERE table1.id = 1 or table1.id = 2
I have two tables i am trying to run a query on Table1 Table2
I run a SELECT query on Table1 to return all rows that match a specific date.
I then insert all the returned rows into Table2.
I then want to UPDATE Table1 timestamp after the INSERT
The problem I am having is with the UPDATE part.
Here is what I have:
INSERT INTO Invoices(...)
UPDATE SiteInvoice SET LastInvoiceDate = CURDATE()
SELECT ... FROM SiteInvoice
WHERE lastinvoicedate IS NOT Null AND LastInvoiceDate> CURDATE() - INTERVAL InvoiceFreq WEEK
As pointed out by juergen d, I can not update and insert at the same time. I have added a trigger to the invoice table to update after insert