i am trying to insert multiple rows using a subquery but it gives and error "SubQuery Returns more than 1 rows"
scenario is that i want to add comment against every test of a subdepartment, i am getting all test id's via subquery but i not being able to iterate over id's and insert comment against each test. Here is my SQL Query
INSERT INTO dc_tp_comment (labid,branchid,Comment,testid,lastupdated,enteredby)
Values('abcd',101,'comment here',(select T.testid
from dc_tp_test T
Inner Join dc_tp_subdepartments S
on T.subdepartmentid = S.subdepartmentid
Where S.subdepartmentid = 13),sysdate(),1)
You cannot use subselect in just one column, use them for whole row:
INSERT INTO dc_tp_comment (labid,branchid,Comment,testid,lastupdated,enteredby)
select 'abcd',101,'comment here', T.testid, sysdate() , 1
from dc_tp_test T Inner Join dc_tp_subdepartments S
on T.subdepartmentid = S.subdepartmentid
Where S.subdepartmentid = 13
Use select insted of values if you want to insert multiple rows...
Syntax would be like:
INSERT INTO dc_tp_comment (labid,branchid,Comment,testid,lastupdated,enteredby)
select 'abcd',101,'comment here',(select T.testid
from dc_tp_test T
Inner Join dc_tp_subdepartments S
on T.subdepartmentid = S.subdepartmentid
Where S.subdepartmentid = 13),sysdate(),1
Related
I have 3 tables that I am using and need to make a query to return data from one table based on the value of a single column in the second table.
tbl_user
ID
login
pass
active
mscID
tbl_master
ID
name
training_date
MSCUnit
Active
tbl_msc
mscID
mscName
my current SQL statement:
SELECT
tbl_master.ID,
tbl_master.name,
tbl_master.training_date,
tbl_master.MSCUnit,
tbl_master.active,
tbl_user.mscID
FROM
tbl_master,
tbl_user
WHERE
tbl_master.active = 1 AND tbl_master.MSCUnit = tbl_user.mscID
The values stored in tbl_msc.mscID is a varchar(11) and it contains a string similar to A00 or A19. This is also the Primary key in the table.
The values stored in tbl_user.mscID matches that of tbl_msc.mscID. The values stored in tbl_master.UnitMSC also matches that of tbl_msc.mscID.
My goal is to return all records from tbl_master where the currently logged in user has the same mscID. The problem I am having is the statement returns all records in tbl_master.
I have tried several different join statements and for some reason, I cannot get this to filter correctly.
I am missing something. Any assistance in the SQL statement would be appreciated.
Thanks,
Will
You should be writing this using joins. I don't know how you know who the current user is, but the idea is to join the three tables together:
SELECT m.ID, m.name, m.training_date, m.MSCUnit, m.active,
u.mscID
FROM tbl_master m JOIN
tbl_user u
ON m.MSCUnit = u.mscID JOIN
tbl_msc msc
ON msc.mscID = u.msc_ID
WHERE m.active = 1 AND msc.mscName = ?;
Notice the use of proper, explicit, standard JOIN syntax and table aliases.
Select a.*, b.userid from
table_master a, table_user b where
a.mscunit in (select mscid from
table_user where active=1)
This should point you in the right direction.
What I am trying to do is to insert records from multiple tables into 1 table by joining. Here is what I have and I can't seem to get it to work. I get the following error #1136 - Column count doesn't match value count at row 1
INSERT INTO users(`name`, `email`,`location_id`, `department_id`)
VALUES('John Doe', 'jdoe#email.com', 1, 1)
INSERT INTO extensions(`ext`)
VALUES(98765)
INSERT INTO dids(`did`)
VALUES('1-800-555-5555')
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT extensions.*, dids.*, users.*
FROM extensions tbl_ext, dids tbl_dids, users tbl_users, users_numbers usn
Inner Join users ON users.id=usn.user_id
Inner Join extensions ON extensions.id=usn.ext_id
Inner Join dids ON dids.id=usn.did_id
WHERE tbl_users.name = 'John Doe'
AND tbl_users.email = 'jdoe#email.com'
AND tbl_ext.ext = 98765
AND tbl_dids.did='401-559-9999';
The first 3 insert statements work. The error fires on the 4 insert when I try to join. Can anyone help.
Use LAST_INSERT_ID():
INSERT INTO users(`name`, `email`,`location_id`, `department_id`)
VALUES('John Doe', 'jdoe#email.com', 1, 1);
SET #user_id = LAST_INSERT_ID();
INSERT INTO extensions(`ext`)
VALUES(98765);
SET #ext_id = LAST_INSERT_ID();
INSERT INTO dids(`did`)
VALUES('1-800-555-5555');
SET #did_id = LAST_INSERT_ID();
INSERT INTO users_numbers(user_id,ext_id,did_id)
VALUES (#user_id,#ext_id,#did_id);
Instead of using user variables (#user_id,#ext_id,#did_id) you can also fetch the LAST_INSERT_ID in your application language.
There are two problems.
First, the SELECT list has to return only the columns that you want to insert into the table. The error is because you're returning tablename.* rather than specific columns.
Second, the SELECT query is very wrong. You have the tables that you're joining listed twice: first in the FROM clause and then again in the INNER JOIN clauses. You should only list them once (unless you're doing a self-JOIN, which is not needed here).
And you shouldn't be joining with users_numbers -- that's the table you're trying to add to, so it doesn't have the IDs for these rows yet.
What you want is a simple cross join between the rows from each of the source tables that match the values you're combining into the relation table.
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT u.id, e.id, d.id
FROM users AS u
CROSS JOIN extensions AS e
CROSS JOIN dids AS d
WHERE u.name = 'John Doe'
AND u.email = 'jdoe#email.com'
AND e.ext = 98765
AND d.did='401-559-9999';
You need to be very specific about which columns you want from that SELECT. Right now you're grabbing everything from the three tables, that's too much.
Maybe you mean:
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT users.id, extensions.id, dids.id
FROM ...
When composing complicated queries like this, run the SELECT subcomponent separately to ensure it produces the right data. I bet if you ran your version it'd show a bunch of columns.
I have tree table
a (a_col1, a_col12, a_col3)
b (b_col1, b_col12, b_col3)
c (c_col1, c_col12, c_col3)
I want to write the b.b_col3 to c.c_col3
where a.a_col1 equals to b.b_col12.
What am I doing wrong ?
INSERT INTO c(c_col3)
SELECT a.a_col1, b.b_col12
FROM a LEFT JOIN b
ON
a.a_col1 = b.b_col12;
You are trying to insert 2 columns value in single column, use something like below-
INSERT INTO c(c_col2,c_col3) SELECT a.a_col1, b.b_col12 FROM a LEFT JOIN b ON a.a_col1 = b.b_col12;
You can not do both stuff with one query. You cannot INSERT and SELECT at the same time. Try first selecting and then inserting if it is possible.
Hi I want to update a table with the values of another but do not how to do it.
I have tried this but it is not working.
UPDATE tblagendamiento SET
CodigoAgenda =
(select tmptable.CodigoCita from tmptable where tmptable.id = tblagendamiento.id);
And this is the error:
Subquery returns more than 1 row
You are actually getting an error because your subquery is returning more than one row. I think you can achieve this simply using an INNER JOIN query
UPDATE tblagendamiento a
INNER JOIN tmptable b
ON a.id = b.id
SET a.CodigoAgenda = b.CodigoCita
The message is telling you that there is more than one row returned by your subquery. Assuming you don't want to use a random value (which you can do by appending limit 1 to the query), it means your where clause is not selective enough.
Is there a way to update multiple rows with different values for each row using a single SQL query? I have to update one colum in many rows with different data. Using individual update queries for each row seems excessive so if it's possible I would like to consolidate this process into a single SQL statement or at least reduce the number of queries required.
I am using PHP with the Zend framework and MySql.
Create a temporary table and fill it with:
CREATE TEMPORARY TABLE temptable (id INTEGER, VALUE VARCHAR(200))
INSERT
INTO temptable
VALUES
('1', 'val1'),
('2', 'val2'),
('3', 'val3'),
('4', 'val4')
Then issue:
UPDATE
mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id
Don't know about MySQL specifically, but to update multiple rows based on a SELECT, or a UNION of multiple SELECTs, I would do
UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
Update 10/28/2014, converted to work for MySQL:
UPDATE MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
SET MyColumn = T.OtherColumn
I know this works for SQL Server, so it's worth a try in MySQL.
update xtable
set a =
Case
when a = "a"
then z
when a = "b"
then y
End
where ...
You can construct the case statement based on your different rows.