How to Insert values into one table from all tables from database - php

I have problems with the last line of the code:
if(isset($_POST['kolona']))
{
foreach($_POST['kolona'] as $vrednost)
mysql_query("ALTER TABLE tablica ADD $vrednost text NOT NULL");
mysql_query("INSERT INTO tablica ( ".(implode(',',($_POST['kolona']))).") SELECT ".(implode(',',($_POST['kolona'])))." FROM druga");
}
First query is making columns in table 'tablica' and second query suppose to insert values in that columns from all tables from which are the columns, for now it's just hard coded, it's only from table 'druga', but i don't know how to go through the all tables, not just 'druga'. I tried with a loop and also with implode function but nothing seems to be working. Can anyone help?

You can tackle merging problems like this using the ON DUPLICATE KEY feature:
INSERT INTO target (id, column1, column2)
SELECT id, column1, column2 FROM source
ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)
It works well provided you have a PRIMARY KEY column without conflicts between your source and target tables.

Related

Copy all columns from one table to another table BUT few columns need to be specified php variables

I want to copy columns from one table to another which is not the problem when I want to copy "AllInOne".
I will write:
INSERT INTO table2 SELECT * FROM table1 WHERE condition;
If I need to copy only part of the columns I will write:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ... FROM table1 WHERE condition;
BUT
What if I need to copy all columns, but I need two or three columns with specified PHP variables that I am getting from $_POST[] or $_GET[]?
If I will have in the table only maybe ten columns together I will write it "manually" which is my second example. But I have more than 30 columns in my tables....
Therefore I am trying to find some combination where I can INSERT * and also specify column2, column3 for example.
Is there some way how to do that? Something like this:
NSERT INTO table2 ( *, Price, Customer) SELECT ( *, '451', 'John')
FROM table1 WHERE condition;
Therefore I am trying to find some combination where I can INSERT * and also specify column2, column3 for example
This is really more of a SQL question then, if I'm not mistaking something? If your question is about binding variables in PHP then that's a different story, but it seems from your question that you know how to do that already.
SQL doesn't support a syntax that's like, "* for all the columns except for these columns." You'll just have to list out every column in the query. Using INSERT INTO ... SELECT * won't work for what you want to do, and is considered bad practice anyway (for example, if you drop or add a column from the table later, the query to select one into the other will break).
If I will have in the table only maybe ten columns together I will write it "manually" which is my second example. But I have more than 30 columns in my tables...
Is that really so bad though? It's a small investment for code that works :)
You are close. Just don't use the parentheses:
INSERT INTO table2 (column1, column2, . . ., price, customer) -- list all columns here
SELECT t1.*, -- I advise you to list the columns here too
451, 'John'
FROM table1 t1
WHERE condition;

Inserting Columns Without Duplicated Null Data in MySQL?

i don't know mysql very much. And i've problem about that. I've a database and it's 20 GB. I want to combine 4 columns and then move the combined column to the new one. But the problem is duplicated data in the table.
For example i wanna combine;
Column1(Not Null),
Column2(Some of them null, some of them not null),
Column3(Not Null),
Column4(Some of them null, some of them not null).
And my new column, which i want to move my combined columns, is fully empty. After my longly research, at last i find this code on dev.mysql.com
INSERT INTO my_table (new_content)
SELECT Column1
FROM my_table WHERE my_table > 0;
As a result, it moved Column1 to the new_content. But my the other 20 columns were duplicated too, as empty fields. How can i make it in an easy way?
Sorry for my bad English. Thanks in advance.
If you want a resulting column based on the string concatenation fo the column you can use concat
INSERT INTO my_table (new_content)
SELECT concat(Column1 , Column2, Column3, Column4)
FROM my_table ;
Create the column in already existing table:
ALTER TABLE my_table ADD COLUMN new_content VARCHAR(55);
Update table and concate all the columns to the newly created column:
UPDATE my_table SET new_content = CONCAT(Column1, Column2);
Create a trigger for all inserting values in future as well:
CREATE TRIGGER insert_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
SET new.new_content = CONCAT(Column1, Column2);
You can also create a trigger for UPDATE:
CREATE TRIGGER
BEFORE UPDATE ON my_table
FOR EACH ROW
SET new.new_content = CONCAT(Column1, Column2);

Omitting Primary Key from PHP MySQL result

I have a small PHP Mysql function which generates all the columns within a mysql table, but I would like the function not to display the primary keys for each table just the other columns.
How can this be done, I havent been able to find the code for it.
Thanks
It seems I didnt explain the question well.
The mysql table from which the columns are generated is sent on demand from a list of ALL THE TABLE IN THE DB (over 150) and I cant specify the exact columns for each of the table.
It would just be more efficient if I found a way of omitting the primary key from the result.
Since it isnt required for the subsequent processing and quite confusing to the enduser as to its use.
Thanks
Use:
SELECT column1, column2, column3 FROM table
For what it's worth, returning the PK or not isn't going to break the bank.
In general, doing SELECT * FROM is bad, but if you're just going to do SELECT every, column, but, the, pk FROM then you may as well just select everything.
The best answer is just to SELECT the columns you need. If you need 3 columns, query for 3 columns and name them explicitly: SELECT column1, column2, column3 FROM table_name

How to Update if the data exist else insert the new data (multiple rows)

I need to create a insert and update statement, when today date is not in the database it will insert else it will update the QTY (from excel [this part I have done]) get from today.
But, there have a lots of row need to be insert and update.
1) it will check for the last 4 days in database, if there doesn't include today, it will just insert the data for today and update the last 3 days data. in the other hand, if there contain today it will just update.
P.S: I had try to use INSERT... ON DUPLICATE KEY UPDATE but it only 1 row affected.
If else statement , when i used this it only insert one row of data then the rest it just doing update.
Can give me some advise or example.
suppose you bulk copy your data from excel to a temporary table tbl and your actual table is tbl1 then do something like this
begin transaction;
if not exists(select * from tbl(updlock holdlock) where...)
begin
insert into tbl1...
else
begin
update tbl1...
end
commit;
What language are you using to do this? I have done something similar in Ruby before. I would make the column (Date in your case) unique at the database level then simply try inserting each record. When I get an exception thrown because the Date is not unique I would then proceed to update the QTY.
I found this article on mysql which says it supports multiple insert.
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
That statement is identical to the following two statements:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=3;
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;
So if we want to edit straight, we could do something like this.
INSERT INTO table (uniquekey,data) VALUES (1,2),(4,5)
ON DUPLICATE KEY UPDATE data=VALUES(data);

PHP, MySQL: Duplicate series of rows, but change 1 column?

I have a table called scheduler_sched which has several columns, including a column called schedule_id.
I need a function where I can pass 2 ids (copy_from_id, copy_to_id) as parameters. And what I need to do is take every row where schedule_id = copy_from_id AND duplicate it but change the copy_from_id to the copy_to_id
So basically I want to to the equivalient of this:
UPDATE scheduler_sched SET schedule_id = 32 WHERE schedule_id = 28
Only I do not want to UPDATE any rows, I want to create duplicates with the new ID's
Does this make sense?
How can I do this?
THANKS!
(By the way schedule_id is not a unique/index field on this table)
Insert into scheduler_sched (column1, column2, column3,schedule_id )
Select column1, column2, column3, 32 from scheduler_sched WHERE schedule_id = 28
I think that ON DUPLICATE KEY UPDATE syntax may help you:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
e.g.:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Just INSERT a new row instead of updating. SELECT first if that schedule_id 28 exists, and if it does, insert a new one with that being the ID.
Since you haven't specified a version of MySQL, I'm going to assume that it is the lastest (5.4).
Assuming I am understanding you correctly, you should be able to implement this using triggers: http://dev.mysql.com/doc/refman/5.4/en/create-trigger.html
One of the benefits of using triggers, is it is all handled by the database itself.

Categories