Mysql update column - mysql

I have 2 tables fullInfo and fundInfo. fullInfo is a full data set of donations to a non-profit. fundInfo is a list of unique fund subgroups with accompanying id numbers. I'm trying to insert the fund id number from fundInfo into fullInfo in a column fundId that exists but currently has NULL values.
fullInfo:
id funddesc amount fundId
002 GENERAL 25.00 NULL
044 MAINT 50.00 NULL
122 TRAVEL 75.00 NULL
... ... ... ...
fundInfo:
id funddesc
01 MAINT
02 TRAVEL
03 GENERAL
... ...
update fullInfo
set fullInfo.fundId = fundInfo.id
where fullInfo.funddesc = fundInfo.funddesc;
This code is not working. Any suggestions?

update fullInfo
set fullInfo.fundId = fundInfo.id
from
fundInfo
where fullInfo.funddesc = fundInfo.funddesc;

UPDATE fullInfo
INNER JOIN fundInfo
ON fullInfo.funddesc = fundInfo.funddesc
SET fullInfo.fundId = fundInfo.id;

Related

MySQL Trigger Setting All Other Values to NULL When Run

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.

How to update one table in MYSQL from another table?

I have two tables
Table tool
column names:
id toolnumber currentduedate
1 123 11/3/2015
2 456 11/3/2015
3 789 11/3/2015
Table event
column names:
id eventnumber newDuedate
7 123 11/3/2015
9 123 11/3/2015
10 456 11/3/2015
What i want is when i update the newDuedate in table event it should update the currentduedate in tool table.
I am using this query:
mysql_query
UPDATE tool INNER JOIN event SET tool.currentduedate = event.newDuedate WHERE tool.toolnumber = event.eventnumber ;
is working fine but if i have 2 field with the same eventnumber this query update only one. Any ideas?
Try this way
UPDATE tool
INNER JOIN event on tool.toolNumber = event.eventnumber
SET tool.currentduedate = event.newDuedate ;

How to update the each record in sql server

I need to update each records in the table using the Update script
query 1:
Select acheivementsId
from Students
where student_id = 2
Result:: 61 records// Number of acheivementsid
query 2 :
Select acheivementsId
from Students
where student_id = 4
Result: 61 records// Number of acheivementsid
I need to update Student_id = 2 acheivementid's with student_id = 4 acheivementId's.
how to write the Update Statement for updating 61 records.
Thanks
Try this:
UPDATE Students
SET acheivementsId = (SELECT acheivementsId from Students where Student_id=4)
WHERE Student_id=2

Using 'AND' in SQL WHERE clause for the same filed

Here is the scenario. I have 1 table that has all the contact details. Another table that has all the list of Categories. And a 3rd table which is an associate table, that has the ID of the first table and the ID of the second table.
This is how my associate table looks like
contactdid -2 | categoryid -1
contactdid -2 | categoryid -2
contactdid -2 | categoryid -3
contactdid -3 | categoryid -1
contactdid -3 | categoryid -3
This is my SQL code below(Generated using SQLyog and i included the where clause).
SELECT
press_contacts.email
FROM
contacts_category
INNER JOIN press_category
ON (contacts_category.categoryid = press_category.id)
INNER JOIN press_contacts
ON (contacts_category.contactdid = press_contacts.id)
WHERE contacts_category.categoryid = 1 AND contacts_category.categoryid = 2 ;
I get the output when I do not have AND contacts_category.categoryid = 2inserted in the code.
Any idea how to solve this.I clearly have data.
Thanks in advance for the help.
contacts_category.categoryid can not be 1 and 2 at the same time, perhabs you mean OR instead of AND?
Use OR or IN() instead of AND.
A field can't have two values at the same time
If you only want to see those email addresses with contacts in both categories, try:
SELECT press_contacts.email
FROM contacts_category
INNER JOIN press_contacts
ON (contacts_category.contactdid = press_contacts.id)
WHERE contacts_category.categoryid in (1, 2)
GROUP BY press_contacts.email
HAVING COUNT(DISTINCT contacts_category.categoryid)=2

sql ms access select by date and genre

I have two tables:
fID Enamn Fnamn bdate
1 Ronge Paus 49-05-14
2 Nesser Håk 50-07-26
3 Bods Thomas 51-05-02
And
ISBN cathegory
123 Prosa
456 Poesi
789 Thriller
I am trying to select by date and cathegory. The writer shoult be born before year "1950" and the cathegory should be "Poesi"
The result should be:
ISBN cathegory fID Enamn Fnamn bdate
456 poesi 1 Ronge Paus 49-05-14
I have tried the followin:
SELECT *
FROM bok, författare
WHERE bdate BETWEEN #01/01/40# AND #01/01/50# AND kategori = 'poesi'
But it didn't work :(
You need a method to match a record from the first table to it's category in the second table. I assumed ISBN is the primary key in the second table, so used it as a foreign key in the first table.
I am unclear about the table names, so I substituted my own names. Notice I also assumed bdate is Date/Time data type. Here's what my versions of the tables look like:
tblA:
fID Enamn Fnamn bdate fkey_ISBN
1 Ronge Paus 49-05-14 456
2 Nesser Håk 50-07-26 123
3 Bods Thomas 51-05-02 789
tblB:
ISBN cathegory
123 Prosa
456 Poesi
789 Thriller
Then, to get writers born before year 1950 for cathegory "Poesi", use this query:
SELECT
a.fkey_ISBN,
b.cathegory,
a.fID,
a.Enamn,
a.Fnamn,
a.bdate
FROM
tblA AS a
INNER JOIN tblB AS b
ON a.fkey_ISBN = b.ISBN
WHERE
a.bdate < #1950/01/01#
AND b.kategori = 'poesi';
Edit: If ISBN is in fact the primary key of tblB, you may get better performance by using ISBN rather than kategori in your WHERE clause:
WHERE
a.bdate < #1950/01/01#
AND b.ISBN = 456
or if IBSN is text rather than numeric data type:
WHERE
a.bdate < #1950/01/01#
AND b.ISBN = '456'