pdo - How INSERT table value in Select and additionally string - php

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)

Related

Sql INSERT INTO combining value and select failure

I wanted to insert two values, one is filled by a fixed number and the other one is the id from another table.
Now I got the error
#1242 - Subquery returns more than 1 row.
INSERT INTO table1 (value1, value2) VALUES
(6 , (SELECT id FROM table2 WHERE name = 'Peter'))
Maybe you can help me.
If you want to insert record into your table1 for every record in table2 where name is Peter this approach should work.
This insert query will insert all records from table2 where name is "Peter" into table1. If you want to insert only one record you could use LIMIT as Macmee has explained in his answer
insert into dbo.table1
(
value1,
value2
)(
select
6,
table2.id
from
table2
where
name = 'Peter'
)
try using LIMIT 1:
INSERT INTO table1 (value1, value2) VALUES (6 , (SELECT id FROM table2 WHERE name = 'Peter' LIMIT 1))
This way in your nested query (SELECT id FROM table2 WHERE name = 'Peter' LIMIT 1) it will only return the first match, and your insert should go through.
Keep in mind that if your intent was to insert new rows for EVERY row in table2 who's name is "Peter" then this will only insert the first one.

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;

SQL - Insertion of row with selection from other row IF the row doesn't exits

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)

PHP, SQL insert value into tables that are linked together?

I have 2 tables,
Table User:
Columns-> id, name, age, position, department, phone
Table Phone:
Columns id, u_id, u_phonetype (foreign key is user.id with on delete cascade and update cascade).
I want to perform this INSERT INTO sql with the u_phonetype from the Phone table:
INSERT INTO `user` (`id`, `name`, `age`, `position`, `department`,
`phone`) values ("'.$name.'", "'.$age.'", "'.$position.'", "'.$department.'", "'.$phone.'") ;
How do I insert into the values of u_phonetype into this SQL? Would I be performing a JOIN?
using php you would just need to use 2 / 3 queries to get this done.
// insert your user
INSERT INTO users (columns) VALUES(vals);
// get the last id
SELECT id FROM users order by id desc limit 1
// this is also work if you just do
SELECT id FROM users where name='name' and age='age' "and whatever values you have";
// then insert into phone with the id retrieved from above
INSERT INTO Phone (columns) VALUES(vals);
3 step process but under the same connection it should be ok
What about
LAST_INSERT_ID();
INSERT INTO table1 (column1,column2) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO table2 (column3,column4,column5) VALUES (#last_id_in_table1, 2, 3);

insert into with select

hello I would like to insert the values ​​in a column from a select query
insert into (cmts) stat_alert
values(SELECT stat_alert.cell,stat_alert.cmts,cell_cmt.cmts FROM stat_alert`LEFT OUTER JOIN cell_cmt ON cell_cmt.`cell`=stat_alert.cell WHERE stat_alert.`cell`=cell_cmt.cell )
I have syntax error
who can help me??
You do not need VALUES keyword and round brackets:
INSERT INTO <table name> (<comma separated column list>)
SELECT <comma separated column list> FROM <table name>
Read the INSERT ... SELECT syntax from the manual.
You want to use INSERT INTO otherTable (cols) SELECT <yourColumns> FROM <yourtable> not INSERT INTO ... VALUES():
insert into stat_alert (cmts) -- list your columns here to insert into
SELECT cell_cmt.cmts -- your values to be inserted are here
FROM stat_alert
LEFT OUTER JOIN cell_cmt
ON cell_cmt.`cell`=stat_alert.cell
WHERE stat_alert.`cell`=cell_cmt.cell
It looks like you have:
The column list before the table name
One column to insert into, but three columns selected
A values keyword when none is needed in this kind of query.

Categories