Copy From One Table To Another Specific Cols And Variables - php

Hi,
I Want to copy from one table to another with the specific cols.
In addition i want to add to the query an external variable. for example in one table i have col1,col2,col3 and in the second table i have the same cols ( col1,col2,col2 )
but now i want to insert to the col4 a variable. it will be like : insert into col1,col2,col3,col4 select (col1,col2,col3,$VARIABLE)
$copy_cols= 'INSERT INTO A (COL1,COL2,COL3,COL4) SELECT COL1,COL2,COL3,$VAR FROM B WHERE Descriptiontable="AAA"';
mysql_cols($copy_query);
the col4 is not can be found in table B , so i want to insert to this col some variable.. for example : the variable can be a constant number.

$copy_cols = 'INSERT INTO A (COL1,COL2,COL3,COL4)
SELECT COL1,COL2,COL3,'FOO' AS COL4
FROM B WHERE Descriptiontable="AAA"';
mysql_cols($copy_query);

Related

How can I UPDATE a column in a record when that information is the result of a GROUP CONCAT in the same table?

I cannot determine if this should be something nested or a JOIN.
Each record has three values from the column value across from their name in the column variable.
I have a successful GROUP CONCAT that combines them into a single text string.
BUT, I need to UPDATE/INSERT the concat value into another variable=>value pair.
Such as this. I want each person to have "cityStateZip" in the value for `cust_abc'.
I believe it's "INSERT" and not update since none of the records have anything in cust_abc yet. But, I'm not quite sure if it shouldn't be UPDATE.
id_member
variable
value
1234
cust_abc
"should show citystatezip"
1234
cust_a
city
1234
cust_b
state
1234
cust_c
zip
I can't get past the error of having the target table being the same in the SELECT FROM.
I was attempting things like:
INSERT INTO smgqg_themes.value (my group concat) WHEN `variable` = "cust_abc"
This is the group concat that works fine to make the string:
SELECT GROUP_CONCAT
( `value`
order by case variable
when 'cust_a' then 1
when 'cust_b' then 2 else 3 end
SEPARATOR '')
output
FROM smfqg_themes
WHERE `id_member` IN (1234, 1235, 1236, etc)
AND `variable` IN ('cust_a', 'cust_b', 'cust_c')
You can INSERT into a the same table you SELECT from in the same SQL statement. I do this all the time.
The syntax is:
INSERT INTO <tablename> (<columns...>)
SELECT ...;
The SELECT must return the same columns that you name in the INSERT clause, in the same order.
If you want an existing row to be updated, but still insert a row if one is missing with the 'cust_abc' varible, then you can use INSERT...ON DUPLICATE KEY UPDATE:
INSERT INTO smgqg_themes (id_member, variable, value)
SELECT id_member, 'cust_abc',
GROUP_CONCAT(`value`
ORDER BY CASE variable
WHEN 'cust_a' THEN 1
WHEN 'cust_b' THEN 2 ELSE 3 END
SEPARATOR '') as v
FROM smgqg_themes
WHERE `id_member` IN (1234, 1235, 1236)
AND `variable` IN ('cust_a', 'cust_b', 'cust_c')
GROUP BY `id_member`
ON DUPLICATE KEY UPDATE value = VALUES(value);
Demo: https://dbfiddle.uk/OPJQ1ZXF

Get LAST_INSERT_ID() from a multi-insert in MySQL

I need to get my id value after inserting multiply rows in my MySQL database
INSERT INTO table (`row1`, `row2`, `row3`, `row4`)
VALUES
('value1','value2','value3','value4'),
('value1','value2','value3','value4');
in the fact I need to something like
SQL Server - Return value after INSERT
but for MySQL and I don't need to return last id because I can do it by myself but I thought it is not good for what I doing
is any better way exist for my question?
If you want to insert blocks of rows into the parent and child table, your rows must have unique columns so you can look up the parent rows after insert:
INSERT INTO table (col1, col2, col3, col4)
VALUES
('value1','value2','value3','value4'),
('value5','value6','value7','value8');
INSERT INTO childtable (colx, coly, id_table)
SELECT 1, 2, id FROM table WHERE col1 = 'value1' and col2 = 'value2' and col3 = 'value3' and col4 = 'value4'
UNION ALL
SELECT 1, 2, id FROM table WHERE col1 = 'value5' and col2 = 'value2' and col3 = 'value3' and col4 = 'value4'
Even if there was a method for getting all inserted IDs, you'd have to do the same thing, because tables contain an unordered set of rows. If you got back IDs 11 and 12 for the first insert statement, which row would ID 11 refer to and which ID 12? There is no way to tell that, because the DBMS is free to insert the rows in any order.
If the row data is not unique as in your example, where both rows contain ('value1','value2','value3','value4'), then you must either insert all the data in a loop or use some CTE to number the rows first and play with those numbers.
Anyway, having said all that, I would just insert the data row by row in a loop (i.e. one parent, all its children, next parent, ...), as long as you don't get unbearable performance issues.

Using Multiple WHERE clause in an INSERT INTO ... SELECT statement

Am having two tables inside my Database lets say Table1 and Table2, and am trying to copy the values from a specific rows(fields) in Table2 to a specific single row on Table1, The specific rows to be moved are determined by the User Input of Particular UserID of each row from Table1.
Table Structure:
Table 1: a_uid,a_FName,a_Username,a_PhoneNo,b_uid,b_FName,b_Username,b_PhoneNo......
And Table 2 Structure:
Table 2: uid,FName,Username,PhoneNo.....
Am uing the INSERT INTO .. SELECT statement, but with multiple WHERE clause but its giving me erros
INSERT INTO table1 WHERE uid='userinput1' (b_FName,b_Username,b_PhoneNo,) SELECT FName,Username,PhoneNo FROM table2 WHERE uid='userinput2';
But am getting Error
This type of clause was previously parsed. (near WHERE)
If the row in table1 already exists, you need an UPDATE .. JOIN statement instead of INSERT .. SELECT.
UPDATE table1 t1
JOIN table2 t2 ON t2.uid='userinput2'
SET t1.b_FName = t2.FName,
t1.b_Username = t2.Username,
t1.b_PhoneNo = t2.PhoneNo
WHERE t1.uid='userinput1'
If you don't know if the row in table1 already exists, you can use an INSERT .. SELECT .. ON DUPLICATE KEY UPDATE statement:
INSERT INTO table1 (uid, b_FName, b_Username, b_PhoneNo)
SELECT 'userinput1', FName, Username, PhoneNo
FROM table2
WHERE uid = 'userinput2'
ON DUPLICATE KEY UPDATE
SET b_FName = VALUES(FName),
b_Username = VALUES(Username),
b_PhoneNo = VALUES(PhoneNo)
Note that uid sould be primary keys or at least unique in both tables.
For sql-server it would be something like this:
INSERT INTO table1 (uid,FName,Username,PhoneNo)
SELECT #userinput1, FName,Username,PhoneNo
FROM table2
WHERE table2.uid=#userinput2;

Insert into 2 table and set both foreign key auto increment

I have a database with two tables. When a user posts an article, it will be inserted into both tables, (2 queries in one file)
I use post_id as foreign key, both tables post_id auto increment. Will foreign keys be messed up? For example if users A and B query the database at the same time.
Table 1
post_id user...
1 A
2 B
Table 2
post_id content...
1 A
2 B
First off you can't have auto increment on both tables.
Usually, what you do is insert in table 1, get the ID of the just inserted row.
Then you use this ID, to insert in table 2 which references table 1.
See: mysqli::$insert_id at
http://www.php.net/manual/en/mysqli.insert-id.php
Example:
$query = "INSERT INTO table1(user,whatever) VALUES ('A','something')";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
$query = "INSERT INTO table2(post_id,content) VALUES ($mysqli->insert_id,'This is content')";
$mysqli->query($query);
You could also do this using a stored procedure based on: stackoverflow.com/a/1723325/1688441
DELIMITER //
CREATE PROCEDURE new_post_with_content(
user_id CHAR(5), content_text CHAR(100)
BEGIN
START TRANSACTION;
INSERT INTO table1 (user)
VALUES(user_id);
INSERT INTO table2 (post_id, content)
VALUES(LAST_INSERT_ID(), content_text);
COMMIT;
END//
DELIMITER ;
And you call it like so:
CALL new_engineer_with_task('A','This is the content');
Why not use table1 as user table and second with posts?
users
user_id(autoinc) username
1 A
2 B
3 C
posts
post_id(autoinc) user_id posts_text
1 2 text
2 1 other text

SQL command to copy selected content from selected rows into other rows?

Any idea how to copy: name, content from rows where language_id = 1 to rows where language_id = 2?
How should SQL command look like?
I want to achive:
http://dev.mysql.com/doc/refman/5.0/en/insert-select.html is what you need to do
assuming it is the productid that you want to update from lang1 to lang 2
update a set
a.name = b.name,
a.content = b.content
from tablea a
join tablea b on a.productid = b.productid
where a.language_id = 2
and b.language_id = 1
ofcourse this will do it for every row in the table so if you want to restrict it then make sure to restrict it by the productids
Did you mean copying all language_id=1 rows to language_id=2 ones?
My knowledge of MySQL syntax is very poor, so I dare not give you all the codez, but at least you may find the following approach useful:
Create a temp table with the structure like this:
product_id int,
name (varchar?)
content (varchar?)
That is, include product_id and all the columns you need to copy.
Populate the temp table with the language_id=1 data. Probably like this:
INSERT INTO temp_table
SELECT product_id, name, content
FROM orig_table
WHERE language_id = 1
Update those rows in the original table where language_id=2 with the corresponding data in the temp table. It may look like this:
UPDATE orig_table
SET
name = temp_table.name,
content = temp_table.content
FROM temp_table
WHERE orig_table.product_id = temp_table.product_id
AND orig_table.language_id = 2
Insert the rows from the temp table into the original table, where the products don't have language_id=2. Something like this:
INSERT INTO orig_table (product_id, language_id, name, content)
SELECT product_id, 2, name, content
FROM temp_table
WHERE NOT EXISTS (
SELECT 1 FROM orig_table
WHERE product_id = temp_table.product.id
AND language_id = 2
)
If you didn't mean to change the already existing language_id=2 data, then step #3 should be omitted and you might further want to modify step #2 in such a way that it selected language_id=1 data only for the products lacking language_id=2.

Categories