Updating a table column based on another tables results using mysql only - mysql

I have a set of results - SELECT id FROM recruit_index WHERE YEAR NOT LIKE '2011'
I need to update another table column based on each of the above ids using mysql only.
Could anyone point me in the right direction?
Thanks in advance
Mike

Use:
UPDATE recruit_index, other_table set other_table.column={new value here}
WHERE recruit_index.id = other_table.id and recruit_index.year not like '2011';

Related

Show values from a column, using values from another column as conditions

I have a table called "fisketur" that looks like this:
I have a table called "plads" that looks like this:
How can I make it so that I only get the values from "plads.navn" that have a "plads.id" that corresponds to a specific month of the year (as found in the "fisketur" table)?
This is what I tried (getting all "plads.navn" that correspond to a "fisketur" taking place in october (month=10):
The output is correct, BUT I would like to set the the whole output column from line 92 (select plads_id from lystfisker.fisketur where month(dato)=10;) as a condition, and not have to set the output values manually as done in line 93.
Any help is greatly appreciated,
Best regards.
You can do it using subquery :
select navn
from lystfisker.plads
where plads_id in ( select plads_id
from lystfisker.fisketur
where month(dato) = 10
);
Or using an inner join
select navn
from lystfisker.plads p
inner join lystfisker.fisketur f on f.plads_id = p.plads_id
where month(f.dato) = 10

How to add value to a specific row in mysql?

I want to add weight value to row 2. How do I do that? I tried to do the following but it gives me an error:
INSERT INTO name(member_id, weight) VALUES(2,55.6);
What is the mistake that I'm having?
If you're updating a specific row you need to use the UPDATE command
UPDATE talbename
SET weight =55.6
WHERE member_id = 2
You can use the UPDATE clause and use a WHERE condition to target a particular row -
UPDATE fromis_9
SET weight = 2
WHERE member_id = 2;
INSERT INTO clause is used for inserting new records.
To Update an existing record, UPDATE clause is used.
UPDATE fromis_9 SET weight = 55.6 WHERE member_id = 2;
Hi I think you should use INSERT INTO table
where condition(member.id=2)
please refer to https://www.w3schools.com/sql/sql_insert_into_select.asp
Its better to learn how to read documentation than wait for complete answer ;)

Update multiple rows with random timestamp [duplicate]

I currently have an oracle table (lovalarm) containing around 600,000 rows. I need to be able to run a query which will cycle through each row and update a field (lovsiteid) to a random number between 14300 and 17300.
So far I have:
update lovalarm
set lovsiteid = (select TRUNC(dbms_random.value(14300,17300)) FROM dual)
Sadly this picks a random number and then updates all rows with the same number which isn't exactly what I'm after!
Can anyone point me in the right direction?
Many thanks,
Cap
Just not use subquery:
update lovalarm
set lovsiteid = TRUNC(dbms_random.value(14300,17300))
Try this:
update lovalarm set lovsiteid = (select FLOOR(RAND() * (17300 - 14300) + 14300))
works in MySQL

MySQL Create View from 2 different tables and columns

I am trying to create a view of 2 tables.
Currently, I am using the line below and this works fine, but I am getting too much information back, I need to be more selective and choose columns:
first table
wp_cart66_orders and i need to pull
bill_first_name_, bill_last_name
status=, new or shipped etc...
2nd table
wp_cart_66_order_items
description, quantity
I am not sure if I need to create a view or just use this query.
Also, I might need to be pointed in the right direction on how to create it if that is the case.
select wp_cart66_orders.*, wp_cart66_order_items.*
from wp_cart66_orders, wp_cart66_order_items
where wp_cart66_orders.id=wp_cart66_order_items.order_id
and wp_cart66_orders.status = 'new';
Thanks.
Write your selective columns, your join explicitly, and reanme the common column names from two tables.
create view OrderItemsVW
as
select wp_cart66_orders.bill_first_name as Bill_First_Name,
wp_cart66_orders.bill_last_name as Bill_Last_Name,
wp_cart66_orders.Description as OrdersDescription,
wp_cart66_order_items.Description as OrderItemsDescription
from wp_cart66_orders
inner join wp_cart66_order_items
on wp_cart66_orders.id=wp_cart66_order_items.order_id
where wp_cart66_orders.status = 'new';
A view will somehow be faster and can be reused. However, you may also just use your select query.
select wp_cart66_orders.*, wp_cart66_order_items.* from wp_cart66_orders, wp_cart66_order_items where wp_cart66_orders.id=wp_cart66_order_items.order_id and wp_cart66_orders.status = 'new';
this will only work if wp_cart66_orders and wp_cart66_order_items have the same columns.
Use the as operator or list every column you want:
Select table1.col1 AS mycolname, table2.col4 AS mycolname2...
or
Select table1.col1,table2.col4 ...

How can I get MySQL to store the result of a query from a second table in the first one?

I have a list of business centers. Sometimes the data entry people don't have all the information such as a missing city name, but the rest of the information is complete. I have another table of zipcodes. I want to be able to do a zipcode lookup for those addresses which don't have a city, and get the city name. I'm doing this in MySQL.
I'm having difficulty with getting the MySQL for this right. I don't know if it's a syntax issue or the logic of the MySQL is wrong. This is what I have:
update centers city
set centers.city = (
select zipcode_types.primary_city
from centers, zipcode_types
where centers.city="" and centers.zipcode=zip);
This is the error I'm getting from the above MySQL:
ERROR 1093 (HY000): You can't specify target table 'city' for update in FROM clause
What I'm attempting to do, is find the city name from the zipcode_types table and update the
missing city name in the centers table. I appreciate the help, thanks!
You can use the multi-table update syntax:
update centers
inner join zipcode_types on zipcode_types.zip = centers.zipcode
set centers.city = zipcode_types.primary_city
where centers.city='';
Try rewriting your update statement as follows:
update centers set city=(select zipcode_types.primary_city from zipcode_types where enters.zipcode=zipcode_types.zip)
where centers.city=''
as show on this SQL Fiddle page: http://sqlfiddle.com/#!2/3f7b5/1
Give this a shot:
UPDATE centers SET city = (
SELECT primary_city
FROM zipcode_types
WHERE zipcode=city.zip)
WHERE city IS null
OR city ==''';