Here is a small example of what I would like to accomplish.
Table 1: Person
ID
FirstName
HairColor_ID
Table 2: HairColor
ID
Color
I have Query 1 where I give it a color parameter(c) and it returns the ID of that Hair Color
SELECT HairColor.ID
FROM HairColor
WHERE (([HairColor].[Color]=[c]));
Now in Query 2, I have the Person table and the Query 1. In the field, I want to choose ID 2 of person and change its HairColor_ID according to the Query 1 result(which should return an ID), Query 2 is below:
UPDATE Person INNER JOIN Query1 ON Person.ID = Query1.ID SET
Person.HairColor_ID = [Query1]![ID]
WHERE (((Person.ID)=2));
I would assume [Query1]![ID] returns the ID.
Basically all I want to do is update the HairColor_ID by giving the Color Value as a parameter. How would I do this?
Essentially what you want is to update a CROSS JOIN between Person and HairColor. You don't need Query1, and to me at least the mechanism is clearer without it.
PARAMETERS c Text ( 255 );
UPDATE Person, HairColor
SET Person.HairColor_ID = [HairColor].[ID]
WHERE Person.id=2 AND HairColor.Color=[c];
You are trying to join the Person table with Query1 via Person.ID = Query1.ID but Query1.ID isn't the ID of a person but of a haircolor...
This query should change the HairColor of person 2 to the color you enter as the parameter c:
UPDATE Person, Query1 SET Person.HairColor_ID = [Query1].[ID]
WHERE (((Person.ID)=2));
Related
There is a table and that table has a column with a name country and I need to change values at that column in one query instead of multiple queries.
In this query, I change any value with Egypt to be 1
UPDATE subscribers
SET country = 1 WHERE country = 'Egypt';
in this query, I change any value with Qatar to be 2
UPDATE subscribers
SET country = 2 WHERE country = 'Qatar';
Any help to make these two queries in one?
Consider:
UPDATE subscribers SET country =
CASE
WHEN country = "Egypt" THEN 1
WHEN country = "Qatar" THEN 2
ELSE country
END
;
Now imagine doing that expression for many more countries. Instead join to a table that 'maps' data association (a master table of country names). Join on CountryName fields and update destination table CountryName field with ID from 'mapping' table. Convert to number type field. Or play it safe and update to another field and when all looks good, delete the original field.
You can use a case expression in MySQL:
UPDATE subscribers
SET country = (CASE WHEN country = 'Egypt' THEN 1 ELSE 2 END)
WHERE country IN ('Egypt', 'Qatar');
However, I would recommend using a derived table:
UPDATE subscribers s JOIN
(SELECT 'Egypt' as country, '1' as new_country UNION ALL
SELECT 'Qatar' as country, '2' as new_country
) x
USING (country)
SET s.country = x.new_country;
I have 3 tables that I am using and need to make a query to return data from one table based on the value of a single column in the second table.
tbl_user
ID
login
pass
active
mscID
tbl_master
ID
name
training_date
MSCUnit
Active
tbl_msc
mscID
mscName
my current SQL statement:
SELECT
tbl_master.ID,
tbl_master.name,
tbl_master.training_date,
tbl_master.MSCUnit,
tbl_master.active,
tbl_user.mscID
FROM
tbl_master,
tbl_user
WHERE
tbl_master.active = 1 AND tbl_master.MSCUnit = tbl_user.mscID
The values stored in tbl_msc.mscID is a varchar(11) and it contains a string similar to A00 or A19. This is also the Primary key in the table.
The values stored in tbl_user.mscID matches that of tbl_msc.mscID. The values stored in tbl_master.UnitMSC also matches that of tbl_msc.mscID.
My goal is to return all records from tbl_master where the currently logged in user has the same mscID. The problem I am having is the statement returns all records in tbl_master.
I have tried several different join statements and for some reason, I cannot get this to filter correctly.
I am missing something. Any assistance in the SQL statement would be appreciated.
Thanks,
Will
You should be writing this using joins. I don't know how you know who the current user is, but the idea is to join the three tables together:
SELECT m.ID, m.name, m.training_date, m.MSCUnit, m.active,
u.mscID
FROM tbl_master m JOIN
tbl_user u
ON m.MSCUnit = u.mscID JOIN
tbl_msc msc
ON msc.mscID = u.msc_ID
WHERE m.active = 1 AND msc.mscName = ?;
Notice the use of proper, explicit, standard JOIN syntax and table aliases.
Select a.*, b.userid from
table_master a, table_user b where
a.mscunit in (select mscid from
table_user where active=1)
This should point you in the right direction.
I have a table something like below,
ID Name EMail Gender
and there are values already in this table.
Now, I used below query to add age.
alter table tblPerson add Age int
Now, my row structure looks like
ID Name EMail gender Age
Now, I have list of ages. Any query to add these ages into newly created column?
All the ages are sorted according to ID. So no IF logic required. I just need to add column data.
Thanks.
Store the values in a temporary table with the id. Then use a join:
update t join
idages ia
on t.id = io.id
set t.age = ia.age;
You can also do this with a giant case expression, but that is prone to error:
update t
set t.age = (case when id = id1 then age1
when id = id2 then age2
when id = id3 then age3
. . .
end)
where t.age in (id1, id2, id3, . . .);
Have is an example of the problem I'm facing. The database tables are a little different than usual, but needed to be setup this way.
Items: id, order_id, other fields
Items_Drinks: id, drinks, other fields
Orders: id, other fields
Orders_Drinks: id, drinks, other fields
I need to have an update query that will update the Orders_Drinks table with the sum of the Items_Drinks drinks field that have the same order_id as Orders_Drinks id field.
Items: 1 1 ...
Items: 2 1 ...
Items_Drinks: 1 4 ...
Items_Drinks: 2 5 ...
Orders: 1 ...
Orders_Drinks: 1 9 ...
The Orders_Drinks is currently correct, but if I were to update Items_Drinks with id of 1 to 5, I would need an update command to get Orders_Drinks with id 1 to equal 10.
It would be best if the command would update every record of the Orders_Drinks.
I know my database is not typical, but it is needed for my application. This is because the Drinks table is not needed for all entries. The Drinks table has over 5000 fields in it, so if every record had these details the database would grow and slow for no real reason. Please do not tell me to restructure the database, this is needed.
I am currently using for loops in my C# program to do what I need, but having 1 command would save a ton of time!
Here is my best attempt, but it gives an error of "invalid group function".
update Orders_Drinks join Items on Items.order_id=Orders_Drinks.id join Items_Drinks on Items_Drinks.id=Items.id set Orders_Drinks.drinks=sum(Item_Drinks.drinks);
I think this is what you're wanting.
Edited:
UPDATE `Order_Drinks` a
SET a.`drinks` = (SELECT SUM(b.`drinks`) FROM `Items_Drinks` b INNER JOIN `Items` c ON (b.`id` = c.`id`) WHERE a.`id` = c.`order_id`)
That should give you a total of 9 for the Order_Drinks table for the row id of 1.
This is assuming that Orders.id == Orders_Drinks.id and that Items.id == Items_Drinks.id.
You need to do an aggregation. You can do this in the join part of the update statement:
update Orders_Drinks od join
(select i.order_id, sum(id.drinks) as sumdrinks
from Items i join
Items_Drinks id
on id.id = i.id
) iid
on iid.order_id = od.id
set od.drinks = iid.sumdrinks;
Something like this will return the id from the orders_drinks table, along with the current value of the drinks summary field, and a new summary value derived from the related items_drinks tables.
(Absent the name of the foreign key column, I've assumed the foreign key column names are of the pattern: "referenced_table_id" )
SELECT od.id
, od.drinks AS old_drinks
, IFNULL(td.tot_drinks,0) AS new_drinks
FROM orders_drinks od
LEFT
JOIN ( SELECT di.orders_drinks_id
, SUM(di.drinks) AS tot_drinks
FROM items_drinks di
GROUP BY di.orders_drinks_id
) td
ON td.orders_drinks_id = od.id
Once we have SELECT query written that gets the result we want, we can change it into an UPDATE statement. Just replace SELECT ... FROM with the UPDATE keyword, and add a SET clause, to assign/replace the value to the drinks column.
e.g.
UPDATE orders_drinks od
LEFT
JOIN ( SELECT di.orders_drinks_id
, SUM(di.drinks) AS tot_drinks
FROM items_drinks di
GROUP BY di.orders_drinks_id
) td
ON td.orders_drinks_id = od.id
SET od.drinks = IFNULL(td.tot_drinks,0)
(NOTE: the IFNULL function is optional. I just used it to substitute a value of zero whenever there are no matching rows in items_drinks found, or whenever the total is NULL.)
This will update all rows (that need to be updated) in the orders_drinks table. A WHERE clause could be added (after the SET clause), if you only wanted to update particular rows in orders_drinks, rather than all rows:
WHERE od.id = 1
Again, to get to this, first get a SELECT statement working to return the new value to be assigned to the column, along with the key of the table to be updated. Once that is working, convert it into an UPDATE statement, moving the expression that returns the new value down to a SET clause.
I need to use a command like this:
insert into PERSON (hometown)
where person_name="Peter"
select city from COUNTRY
where country_id= 2;
It is obviously wrong!
In a nutshell, a command which will select a particular value (country's city from the record where the id is 2) from one table and add it to another table (person's hometown whose name is Peter), so basically a particular attribute's value from one table to another table's particular row.
What will be the command for that?
This should work:
UPDATE Person SET City=(SELECT City FROM Country WHERE Country_id = 2)
WHERE Person_Name = 'Peter'
Assuming mysql: you want to do something like this.
UPDATE Person, Country
SET Person.City=Country.City
WHERE Person.Person_Name = 'Peter'
AND City.Country_id = 2
http://dev.mysql.com/doc/refman/5.0/en/update.html
If you are doing insert specificly, i would recomend something like this:
insert into PERSON (hometown, person_name)
select city, 'Peter' from COUNTRY where country_id= 2;
Just make sure the ID is unique or you will end up with more then one record named 'peter'.