Update MySQL columns based on other columns - mysql

So, I have a members table that we'll call table_members. Among others, it has a column called email.
There's also a table called table_oldnewemail. It has two columns: old_email and new_email.
I'd like to:
1. Find the row in table_members where table_members.email equals table_oldnewemail.old_email
2. Then replace table_members.email with the corresponding table_oldnewemail.new_email
3. Repeat for all table_oldnewemail values
I feel I have all the necessary parts, but is this even possible with a MySQL query? What would be an otherwise smart way to automate such a process?

Did you try something like this:
UPDATE table_members t INNER JOIN
table_oldnewemail tno
ON t.email = tno.old_email
SET t.email = tno.new_email;

Related

What happen if two sql table have the same column name while trying to write a sql query from the two? [duplicate]

I have a table for users. But when a user makes any changes to their profile, I store them in a temp table until I approve them. The data then is copied over to the live table and deleted from the temp table.
What I want to achieve is that when viewing the data in the admin panel, or in the page where the user can double check before submitting, I want to write a single query that will allow me to fetch the data from both tables where the id in both equals $userid. Then I want to display them a table form, where old value appears in the left column and the new value appears in the right column.
I've found some sql solutions, but I'm not sure how to use them in php to echo the results as the columns in both have the same name.
Adding AS to a column name will allow you to alias it to a different name.
SELECT table1.name AS name1, table2.name AS name2, ...
FROM table1
INNER JOIN table2
ON ...
If you use the AS SQL keyword, you can rename a column just for that query's result.
SELECT
`member.uid`,
`member.column` AS `oldvalue`,
`edit.column` AS `newvalue`
FROM member, edit
WHERE
`member.uid` = $userId AND
`edit.uid` = $userId;
Something along those lines should work for you. Although SQL is not my strong point, so I'm pretty sure that this query would not work as is, even on a table with the correct fields and values.
Here is your required query.
Let suppose you have for example name field in two tables. Table one login and table 2 information. Now
SELECT login.name as LoginName , information.name InofName
FROM login left join information on information.user_id = login.id
Now you can use LoginName and InofName anywhere you need.
Use MySQL JOIN. And you can get all data from 2 tables in one mysql query.
SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1

Creating joins based on range of number value

Could you guys provide me on the situation below?
I have 2 tables.
Table 1 looks like this:
Meanwhile, this is table 2:
I would like to join table 2 to table 1 to lookup the grade for each job based on the upper and lower limit column.
By conceptualizing some of the lovely answers here, I manage to come up with a statement that looks something like this:
FROM table2 LEFT JOIN table1 ON (table2.[score] >= table1.[lower limit]) AND (table2.[score] <= table1.[upper limit])
The statement above manage to join them according to a range, however, for some unknown reasons, some rows from the left table went missing and I could not determine what it is. e.g (2000 rows in table 2, but only 1800 in the query)
I am sure the join is the cause, as if i change the join to a equal left join, 2000 rows appear in the query.
Can someone advice me on this?
Regards,
Guang Yong
Perhaps it would be much cleaner to create a table with values from 1-100 and assign them each on of your categories, and essentially mirroring your table 1.
Then you can do Table 2
SELECT Table1.Grade, Table2.Score
FROM Table2 LEFT JOIN Table1 ON Table2.Score = Table1.Score
This would definitely cover all integers between 0 and 100.
If you are manually inputing the scores, you could also use a data macro as simple as this:
go to Table Tools >> Table >> Before Change
Then use the Set Field Action, and set
Name = Table2.Grade
Value = IIf([Score]>=70,"Good",IIf([Score]<=59,"bad","so so"))
With this ^ everytime you type in a score, it will automatically populate the grade column.
Another option is create a query as follows, that will evaluate each line and assign the proper grade:
SELECT Table2.Score,
IIf([Score]>=70,"Good",IIf([Score]<=59,"bad","so so")) AS Grade
FROM Table2;
Good luck!

Create a new table from multiple tables with identical column names without declaring the column names

I have 3 tables that I'd like to pull data from and use the result set to create a new table. Note that each of these tables have identical column names.
CREATE TABLE smsout_32020Nov2014
AS
(SELECT *
FROM smsout17nov2014b,smsoutnov32014,smsout
WHERE smsoutnov32014.shortcode = 32020
AND smsout.shortcode = 32020
AND smsout17nov2014b.shortcode = 32020);
Problem is that I am getting an error that there are duplicate column names
Is there a work around?
As described in comments on your question, it is unclear what results you are actually trying to obtain. Supposing that the question reflects a complete misunderstanding of join operations, however, it may be that UNION ALL is what you're actually looking for. In particular, if you want all the rows of the three named tables for which the shortcode column has the value 32020, then that would be this:
CREATE TABLE smsout_32020Nov2014 AS (
SELECT smsout17nov2014b.*
WHERE shortcode = 32030
UNION ALL
SELECT smsoutnov32014.*
WHERE shortcode = 32030
UNION ALL
SELECT smsout.*
WHERE shortcode = 32030
)
On the other hand, if the results you are selecting are in fact in the form you want, as you have revised your question to say, then you have no alternative but to assign explicit column names to ensure column name uniqueness (so at least for every column in two of the three base tables). You can do this via aliases in the SELECT statement or via a column list in the outer CREATE TABLE statement (or both). Your original question seemed to say that the workaround you wanted was a way to avoid doing that, but now it just seems to say that you want to fix the error.
It will be a bit simpler to do the patch up in the SELECT statement. Based on your revised starting query, that would be something like this:
CREATE TABLE smsout_32020Nov2014 AS (
SELECT
smsout.*,
m.col1 AS month_col1, m.col2 AS month_col2, ... m.shortcode AS month_shortcode,
c.col1 AS code_col1, c.col2 AS code_col2, ... c.shortcode AS code_shortcode,
FROM
smsout17nov2014b m,
smsoutnov32014 c,
smsout
WHERE smsoutnov32014.shortcode = 32020
AND smsout.shortcode = 32020
AND smsout17nov2014b.shortcode = 32020
);
The columns of your new table will be col1, col2, ... shortcode, month_col1, month_col2, ... month_shortcode, code_col1, code_col2, ... code_shortcode.
Note, by the way, that even if those results are in the form you want (which I find surprising), I have trouble believing that the rows are what you want.

Find matches from 2 tables, change other field?

I have a database with two separate tables. One table (T1) has 400+ values in its only column, while the other (T2) has 14,000+ rows and multiple columns.
What I need to do is to compare the column in T1 to one column in T2. For every matching value, I need to update a different value in the same row in T2.
I know this is pretty easy and straight-forward, but I'm new to MySQL and trying to get this down before I go back to other things. Thanks a ton in advance!
EDIT: Here's what I've been trying to no avail..
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` LIKE `Source`.`SourceID`
EDIT 2:
A little clarification:
apollo.Bottom and apollo.Source are the two tables.
apollo.Bottom is the table with one column and 400 records in that column.
I want to compare Bottom.URL to Source.SourceID. If they match, I want to update Source.CaptureInterval to 12.
You can use the following query to update. But the performance will be much better if you index URL and SourceID columns in both tables as they are being used in the WHERE clause.
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` = `Source`.`SourceID`
You can join the two tables together and do a multiple table update.
Start with something like this:
UPDATE `apollo`.`Source`
INNER JOIN `apollo`.`Bottom` ON `apollo`.`Bottom`.`URL` = `apollo`.`Source`.`SourceID`
SET `apollo`.`Source`.`CaptureInterval` = '12';

mysql insert data from multiple select queries

What I've got working and it's what I need to improve on:
INSERT form_data (id,data_id, email)
SELECT '',fk_form_joiner_id AS data_id
, value AS email
FROM wp_contactform_submit_data
WHERE form_key='your-email'
This just gets the emails, now this is great, but not enough as I have a good few different values of form_key that I need to import into different columns, I'm aware that I can do it via php using foreach loops and updates, but this needs to be done purely in mysql.
So how do I do something like:
insert form_data(id,data,email,name,surname,etc) Select [..],Select [..]....
The data is stored in the most ridiculous way possible, in one table, IN ONE CELL, with a different cell telling what the actual data is: http://drp.ly/Jhx3J (screenshot)
Please help
In your question it isn't obvious where the other data you want is stored but perhaps you are looking for something,like this:
INSERT form_data (id,data_id, email,city)
SELECT A.fk_form_joiner_id AS data_id
, A.value AS email
,B.value AS city
FROM wp_contactform_submit_data A,
wp_contactform_submit_data B
WHERE A.form_key='your-email' AND
B.form_key='your-city' and
A.fk_form_joiner_id=B.fk_form_joiner_id
One could extend this logic to handle a smallish number of multiple fields quite easily - if one or more data value sometimes missing you would have to do a outer join between A and B in order to get all records.
Obviously you have to make a new join for each column but this is required in some form to complete your task anyway.
A index on wp_contactform_submit_data(fk_form_joiner_id,form_key) will make this query fairly efficient as well.
That is not a problem, you just neet to join to wp_contactform_submit_data several times (one for each value):
INSERT INTO form_data (data_id, email, name)
SELECT fk_form_joiner_id as data_id, email, name
FROM (SELECT fk_form_joiner_id, value as email
FROM wp_contactform_submit_data
WHERE form_key='your-email'
) as emails
JOIN (SELECT fk_form_joiner_id, value as name
FROM wp_contactform_submit_data
WHERE form_key='your-name') as names
USING (fk_form_joiner_id)
;