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.
Related
Let's Say I have 2 tables
In Table1 I have 3 columns named as
Table1
Id | Name | Date (Format: 12-01-2019)
1 ABC 22-08-2019
2 XYZ 23-07-2019
Now My Question is How to store the Month wise count in another Table(i.e, Table2)
Expected Result:
Table 2
Month | Count
08/Aug 1
07/July 1
I searched many queries but I didn't find a better one
Can anyone provide me this sql query?
OR
If can you provide SQL query which stores all these count in separate column in Table1 with an extra column
merge into table_2 tgt
using
(select trunc(dt, 'month') dt, count(*) cnt
from table_1
group by trunc(dt, 'month')
) src on (tgt.dt = src.dt)
when not matched then insert (tgt.dt, tgt.cnt)
values (src.dt, src.cnt);
INSERT INTO table2(month,count)
SELECT
MONTH(Date) AS m, COUNT(DISTINCT Id)
FROM
table1 GROUP BY m;
This can be done in one query
Insert into Table2 (Column1, Column2)
Select Count(*), DATEColumn from Table1 group by DATEColumn
I am not sure which database your are using this work on MSSQL.
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;
I have 2 tables let say table1 & table2
table1 contains uniqueId, name1, name2, value fields
table2 contains id, uniqueName, keywords fields
table2.keyworks have comma separate names.
So, what I am trying to do is below.
select * from table1
//1> replace table1.name1 with table2.uniqueName if table2.keywords has
//table1.name1
//2> replace table1.name2 with table2.uniqueName if table2.keywords has
//table1.name2
select *,(case when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name1
when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name2 end)from table1
Try this.
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'";
How can I select one row from that table http://i.stack.imgur.com/27cu9.jpg where values of 'user_1' and 'user_2' may look like
user_1 user_2
1 2
2 1
In other words I want to select a field that contains 2 users with submitted=1 no matter in which field the value is.
Here is a simple query that does this:
select *
from t
where submitted = 1 and 2 in (user_1, user_2)
If I understood your question, I think you need to JOIN the table on itself if you are trying to return rows that have corresponding users (1,2) and (2,1):
select t1.*
from yourtable t1
join yourtable t2 on
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1
SQL Fiddle Demo
If however you are just trying to see if user 2 exists in either of the fields, then look at Gordon's post.
Use this:-
select * from tblname as t1, tblname as t2 where
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1 and t1.user_1<>t1.user_2
EDIT:-
Updated the query so that the rows with the same values do not appear in the result.