I am looking for the best practice for processing this query. I just want to make sure I am doing it right.
Query: Need to select all from database table where [column] = # and copy results to another table.
DELETE FROM table WHERE [column] = #
INSERT INTO table2 (col1, col2, col3)
(SELECT col1, col2, col3 FROM table WHERE [column] = #)
CREATE TABLE newtable LIKE originaltable; #This creates a new table just like original table
INSERT INTO newtable (SELECT * FROM originaltable WHERE column = '#'); #this moves selected records from original table to new created table
Related
He needs to insert a table of values from a select query and additionally string
$query=("INSERT INTO table1 (a,b,c,d) VALUES ('a',SELECT b,c,d FROM table WHERE x=1)");
Could you as him to try the following:
INSERT INTO table1 (a,b,c,d) VALUES (SELECT 'a',b,c,d FROM table WHERE x=1)
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;
So, I'm wanting to insert a row into the table teams_views IF the row doesn't already exist. This is a simple query, but I'm having issues including a selection into this query. Basically, one of the fields in the row is in need of being fetched from another table.
Here's a breakdown of what I am wanting to build in SQL:
INSERT INTO
teams_views (col1, col2)
VALUES
(SELECT col1 FROM teams WHERE teams.identifier = 1234, col2)
WHERE
teams_views.col1
IS NULL
What can I do to get this query work?
Thanks.
INSERT INTO teams_views (col1, col2)
SELECT t.col1 , t.col2
FROM teams t
WHERE t.identifier = 1234
AND NOT EXISTS (SELECT 1 FROM teams_views
WHERE t.col1 = col1)
I have 2 tables:
Table1
Table2
When I move a row from table1 to table2, I also want to update the datetime field and 1 more field.
Say both table have identical column like this:
id
shipped_by
datetime
other_column
I have the following sql line, but it is not working of course. But I want to have it something like that.
$query = "INSERT INTO table2
SELECT * FROM table1
WHERE id = '$id' UPDATE table2
SET shipped_by='$shipped_by', datetime='$datetime'";
The variable $shipped_by selects the userid, and $datetime date from now.
Can anyone help me with this sql code to make it work? I cannot figure it out.
Thank you.
To insert data form table1 with some column data modified can be done with insert and select without update.. select * should be used here, each column must be listed except for modified ones..
$query = "INSERT INTO table2
SELECT id, '$shipped_by', '$datetime', other_column FROM table1
WHERE id = '$id'";
My query in PHP consist of two parts: creating temporary table and the main query.
How should I write this query in PHP?
//Creating temporary table part:
create temporary table mytable as(SELECT `Name`, `Date`, `Condition`
FROM info
ORDER BY `Date` DESC );
//The main query part:
select mytable.Name, mytable.Date, mytable.Condition FROM mytable
right join info on mytable.Name = info.Name
Group by mytable.Name;
Edited: My query contain 3 columns(Name, Date and Condition). I need the rows with max(Date), since I have some rows with the same Name, I should use Group.
The following query does not help:
SELECT `Name`, max(Date), `Condition`
FROM info
Group by Name
ORDER BY min(Date) DESC