Hi I am new to SQL database.
I have two tables one is a "Master" and other is "Sub" like this
Master
uid(primary key) f_name l_name
1 fAaa lAaa
2 fBbb lBbb
second table
Sub
tid(primary key) uid(foreign key) time is_free
1 1 1:00AM 0
2 1 2:00AM 1
3 1 3:00AM 0
4 2 1:30PM 0
5 2 2:30PM 1
from both table we can say that user fAaa lAaa is free at 2:00AM and NOT free at 1:00AM and 3:00AM.
now I want to update like this, for user 1(fAaa lAaa), I want to delete time 2:00AM and want to insert new two time like 5:00AM and 6:00AM for user 1 than what should be my join query for update.
please help me!
Thanks
Like this?
DELETE FROM secondtable WHERE uid = 1 AND (time = "1:00AM" OR time = "2:00AM");
INSERT INTO secondtable (uid, time) VALUES (1, "5:00AM"), (1, "6:00AM");
Or
UPDATE secondtable SET time = "5:00AM" WHERE uid = 1 AND time = "1:00AM";
UPDATE secondtable SET time = "6:00AM" WHERE uid = 1 AND time = "2:00AM";
This is some pretty basic stuff, I recommend you do a search for "sql delete from", "sql insert into", "sql update" and look for beginner tutorials.
Related
I have two tables, Accounts and Person:
CREATE TABLE Person(
id INT NOT NULL PRIMARY KEY,
Person_Name VARCHAR(17) NOT NULL,
P_Location INT NOT NULL
);
INSERT INTO Person VALUES (1,"Adam",300),(2,"Betty",10),(3,"Louis",60);
CREATE TABLE Accounts(
Person_id INT PRIMARY KEY,
Balance INT DEFAULT 200);
INSERT INTO Accounts VALUES (1,2000),(2,1350),(3,800);
And one trigger, Bonuses:
CREATE TRIGGER Bonuses
AFTER UPDATE ON Person
FOR EACH ROW
UPDATE Accounts
SET Balance = CASE WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 3 THEN Balance - 150
WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 7 THEN Balance + 100
WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 15 THEN Balance - 30
WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 1 THEN Balance + 200
END;
And I want to make the trigger update the Accounts table according to certain instructions whenever the P_Location on the Person table changes to one of a select few values (3,7,15 and 1). However, as things are they result is incorrect. Assume I run the above code, the tables I get are:
Person
id
Player_Name
P_Location
1
Adam
300
2
Betty
10
3
Louis
60
Accounts
Person_id
Balance
1
2000
2
1350
3
800
Now if I run UPDATE Person SET P_Location = 3 WHERE id = 1; then the Accounts table should yield:
Person_id
Balance
1
1850
2
1350
3
800
However, what I get is
Person_id
Balance
1
1850
2
NULL
3
NULL
Any idea of what I'm doing wrong?
Well, that code did exactly what you said, though it wasn't what you meant!
That's the thing about UPDATE queries, EVERY row will get an update unless a WHERE clause is used to filter what actually gets modified. Nothing is found from the CASE with most records, so any of those will get assigned to NULL. To see this behavior, check this fiddle example.
However, there is good news, all that is needed in the trigger is to add a WHERE clause. Note that I simplified the CASE handling make use of the UPDATE trigger's NEW references:
CREATE TRIGGER Bonuses
AFTER UPDATE ON Person
FOR EACH ROW
UPDATE Accounts
SET Balance = CASE WHEN NEW.P_Location = 3 THEN Balance - 150
WHEN NEW.P_Location = 7 THEN Balance + 100
WHEN NEW.P_Location = 15 THEN Balance - 30
WHEN NEW.P_Location = 1 THEN Balance + 200
END
WHERE Person_id = NEW.id;
So starting with:
Then run: UPDATE Person SET P_Location = 3 WHERE id = 1;
Gives:
Example fiddle with your tables, the simplified trigger case handling, and the output examples from the update query.
i need to take the appropriate action depending on whether the values are found, partially found or not available at all.
This is my table:
id. emp_code. ot_date. ot_hours
1 123 2021-05-01 3
2 567 2021-05-01 1
Now i have these 3 data:
Data #1
emp_code: 123
ot_date: 2021-05-01
ot_hours: 3
Data #2
emp_code: 123
ot_date: 2021-05-02
ot_hours: 3
Data #3
emp_code: 567
ot_date: 2021-05-01
ot_hours: 2.5
This is my logic to decide and take action:
select emp_code, ot_date where values are (emp_code, ot_date)
from ot_forecast
if it exist
select emp_code, ot_dat,ot_hours where values are (emp_code, ot_date, ot_hours)
from ot_forecast
if exist then exit
else update values (emp_code, ot_date, ot_hours)
else insert values (emp_code, ot_date, ot_hours)
Based on my logic, data 1 will be skipped, data 2 will be inserted and data 3 will be updated
Problem is, I can't figure out how to put this in codes. I'm already stuck at the first line.
Hope someone can guide me with this.
Thanks
If you create emp_code,ot_date as a unique (or primary (id seems pointless)) key, that enables INSERT ON DUPLICATE KEY UPDATE.
CREATE UNIQUE INDEX ot_forecast ( emp_code, ot_date)
Your query will become:
INSERT INTO ot_forecast
SET emp_code = xxx, ot_date= yyyy, ot_hours = zzzz
ON DUPLICATE KEY UPDATE ot_hours = zzzz
The first case of yours as the select will incur an update and maybe this is ok as its effectively a no-op.
If you where using MariaDB you could also use INSERT RETURNING:
INSERT INTO ot_forecast
SET emp_code = xxx, ot_date= yyyy, ot_hours = zzzz
ON DUPLICATE KEY UPDATE ot_hours = zzzz
RETURNING emp_code, ot_date, ot_hours
I have a big table with duplicate keys that I am trying to connect to smaller table that has unique keys. I know for a fact there will not matches for everything. I only want a match from my smaller table to update a single row in the bigger table and then to move onto the next smaller table row for the next update. I need it like this because I am trying to create unique id's in the larger table as each row represents a real world product which has it's own heiarchy of real world objects.
So for example,
bigtable
barcodeSnippet t_stamp workId parentCase newId
aaaa time1 1 1 NULL
aaaa time1 1 1 NULL
aaaa time1 1 1 NULL
and my small table might have this
smalltable
id barcodeSnippet t_stamp workId parentCase
1 aaaa time1 1 1
2 aaaa time1 1 1
the end result I want in my bigtable is
bigtable
barcodeSnippet t_stamp workId parentCase newId
aaaa time1 1 1 1
aaaa time1 1 1 2
aaaa time1 1 1 NULL
where I only mached once per row, and was left over with a NULL since I had 3 rows in the big table and two matches in my smaller one.
My current query
UPDATE bigtable as bt
JOIN smallTable as st ON (bt.barcodeSnippet = b.barcodeSnippet AND
bt.parentCase= st.parentCase and bt.t_stamp = st.t_stamp and bt.workId =
st.workId)
SET bt.bottlesId = st.id;
does not work, and I don't see it's possible to use the LIMIT in a UPDATE for MySQL. I have seen other answers in MS SQL where you can use TOP 1, perhaps where newId IS NULL, but again I am using MySQL here.
I am thinking I might need to use a Stored Procedure/Cursor approach but even with that it seems like I will run into the issue of having to run an update statement and then I am back at square 1.
Any ideas? Using MySQL 5.6.
EDIT: Think I have a decent solution. I just updated with my query so I do have duplicates. However, now I added a row number column. I plan to join the table on itself and update it if the row number is < the row number, therefore I keep the top ID and can turn the others to null, which is suitable.
Something like this
UPDATE bigtable tb
JOIN bigtable tb2 ON tb.newId = tb2.newId
SET tb.newId = NULL
WHERE tb.rowNumber < tb2.rowNumber;
You use the auto_incremented id column with the superglobal $_GET:
1.php
<?php $var = $row["id"]; ?>
Something
Then in your 2.php
<?php $id = $_GET["id"];
$sql = "SELECT * FROM table WHERE id='$id';";
Of course you need to use prepared statements
I have a table with 7500+ records and every day is inserted about 300 new ones. Each new record can belong to a particular partner where each partner has its own idpartner.
Example of records: (partner A -> idpartner: 1 / partner B -> idpartner: 2 / partnerC -> idpartner: 3)
PartnerA-123
PartnerA-567
PartnerB-999
PartnerB-123
PartnerC-123
What I can not do is an event that runs every 10 minutes which will update the idpartner based on what comes before "-". If it's partnerA-, then idpartner = 1 and so on ...
I'm having to run an update every 10 minutes to keep this updated ... What I do:
update table set idpartner = 1 where name regexp 'partnerA-';
update table set idpartner = 2 where name regexp 'partnerB-';
update table set idpartner = 1 where name regexp 'partnerC-';
How to make an event that update the table every 10 minutes based on names with regexp?
I have query in MS Access that produces a correct records result, but Access refuses to run the Query as a delete query?
Can any one help me rewrite this query to run in access.
Delete Table_A.*
FROM (SELECT Table_A.Main_RecID, Table_A.Fld_Unique_ID, Table_A.Actiontaken FROM Table_A
WHERE Table_A.Actiontaken="MainRecord deleted") AS Tmp_B
LEFT JOIN Table_A ON Tmp_B.Main_RecID=Table_A.Main_Recid
WHERE (((Table_A.Actiontaken)<>"MainRecord deleted"));
If the "Delete" is replaced by a select or I ask for a datasheet view the Query produces what I would expect. Which is a list of the records in the table that have the same Main_RecID as records with Actiontaken field = "MainRecord deleted" but do not have their Actiontaken field equal to "MainRecord deleted".
Access responds with the message "Could not delete from specified tables."
I started with this data in Table_A ...
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
2 1
3 2 MainRecord deleted
4 2 something else
5 3 something else
Note Actiontaken is Null in second row (where Fld_Unique_ID = 2).
Executing the DELETE statement below leaves this data in the table.
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
3 2 MainRecord deleted
5 3 something else
DELETE
FROM Table_A
WHERE
(
Actiontaken<>'MainRecord deleted'
OR Actiontaken Is Null
)
AND
(
DCount("*", "Table_A",
"Main_RecID = " & Main_RecID
& " AND Actiontaken='MainRecord deleted'") > 0
);
If that's not what you're after, please show us sample data before and after DELETE.