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)
;
Related
This SO post is a follow up question to my SO post. I created a new post since the original question was answered and did not want to distract from answer. In experimenting with the previous solution, it got me thinking and thus this SO question was born.
Reference: DB Fiddle where I'm performing an INSERT INTO with Select in MySQL. In playing around with the data, is it possible to grab some data from the destination table matching a PK field (coreID = :org_coreID) and then combining that with a select statement to perform an INSERT INTO with Select using a :new_coreID?
I know this SQL is invalid but I'm hoping the statement below will help explain the data columns I need to insert into the steps table.
INSERT INTO steps(coreid, wolid, venueID,aid, tcount, rCount, pMax, groupID)
SELECT
a.coreID,
IFNULL(s.woLID, 0) as woLID,
s.venueID,
a.aID,
(select tCount, rCount, pMax, groupID from steps where coreID = :orig_coreID)
FROM
activity a Left Join
step s On s.aID = a.aID
WHERE
a.coreID = :new_coreID;
Is something like this possible?
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;
I'm trying to run a query, that compares two tables. I got a table called mw_email_blacklist, this table is holding about 200k records of my blacklisted emails. In my other table, I got my list subscribers, they got a 'status' field, which can equal "Blacklisted" and then they can't receive an email.
Both tables got a 'email' field, so my plan was to do a where clause based on the email field. This however is not really working out. Can somebody maybe help me?
Included SQL:
UPDATE
`mw_list_subscriber` mls,
`mw_email_blacklist` meb
SET
mls.`status` = "Blacklisted";
WHERE
mls.`email` = meb.`email`;
This SQL actually counted something that could be the right amount of emails that should be blacklisted, but I couldn't get it to work when writing it to an update clause.
SELECT count(ls.email)
FROM mw_list_subscriber ls
WHERE EXISTS (
select email from mw_email_blacklist eb
WHERE ls.email = eb.email
)
Thanks in advance!
If i understand your intention correctly, a general approach could be:
UPDATE mw_list_subscriber mls , mw_email_blacklist AS meb
SET mls.status='Blacklisted'
WHERE mls.email = meb.email
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
Assume that I have a single table, with 3 records, [a], [b], [c].
I want to get back a, b, c, until d is created. D can be a duplication of either a, b or c, except the value in one column: x will be different.
N.B: My DB is MySQL, my back-end in ColdFusion and when I say 'user edits' or 'user sees a list', I mean that the user sees a web page list of items to edit. If you think it would be better done programmatically as opposed to the database doing the work, I would appreciate the input.
So what I'd like is that:
User sees a list of a,b,c.
User edits c.
c is duplicated as d, with a different value in column x.
User sees a list of a,b,d.
User edits b.
b is duplicated as e, with a different value in column x.
User sees a list of a,e,d.
etc...
It seems like it should be a case of SELECT [fooRecord] UNLESS [barRecord exists] in which case bring back [barRecord]. I've looked around for similar answers, and some do use EXISTS, but that only seems to return a true or false value, not the record I'm looking for. I believe it's a fancy sub query or INNER JOIN that I need, but I'm at a loss to think up the exact syntax to do it.
This is my first time posting on SO so I apologise if I've done any part of it incorrectly or my question isn't clear, I will provide clarity or further info if asked for. Any help much appreciated.
Your schema seems a bit convoluted - might be time to rethink that. Often the issue is an overwrought attempt to avoid additional tables or columns - but this might a case for additional meta data added to the schema to help pull this data. You might also consider simply selecting ALL the data (a,b,c,d,e) and simply using Q of a Q to tease out the row you want based on X.
Meanwhile, a CASE statement might get you there. This is total untested psuedo-code but it might give you an idea.
<Cfquery>
SELECT
CASE
WHEN x = *condition* THEN orig.col1
ELSE mod.col1
END CASE
AS col1,
CASE
WHEN x = *condition* THEN orig.col2
ELSE mod.col2
END CASE
AS col2
FROM tablename orig OUTER JOIN tablename mod
ON (orig.col1 = mod.col1
AND orig.col2 = mod.col2
AND orig.x <> orig.x)
</cfquery>
What I'm trying to do is join the 2 tables together and tease out one value vs the other based on whether or not it exists. I'm not sure I'm quite there with this query but you get the idea.
Another approach would be a UNION - something like this:
<Cfquery>
SELECT
col1, col2, col3
FROM tablename
WHERE *primarykey* NOT IN
(*select rows that DO have duplicates with modified x*)
UNION ALL
SELECT
col1, col2, col3
FROM tablename
WHERE *primarykey* IN
(*select rows that WITH the modified x*)
</cfquery>
This has the advantage of being a bit easier to understand. You can work with one query, then the other, then join them together to get what you want.
Good luck - seems daunting :)
I think, and I might be wrong so please correct me, that this is a NoSQL pattern. In that I, JaredE exist as a user in the users table, but my userid isn't the primary key of that table and thus anytime I or someone else edits my record it just inserts a new record rather than updates my existing record.
If you were to use this pattern, then you'd be doing something like
select distinct userid, a,b,c, lastmodified
from tbl
ORDER BY lastmodified desc
Arrived on the answer after some discussion with the team:
SELECT *
FROM
table1 t1
WHERE 1=1
AND (
x = <cfqueryparam value="#arguments.x#">
OR (
x is null
and
y not in
(
SELECT t2.et_code
FROM table2 t2
WHERE
t2.x = <cfqueryparam value="#arguments.x#">
)
)
)
I tried to format the answer so that it's easily readable. Thank you for the other answers.