I have two tables with existing data pulled in from Excel spreadsheets converted to .csv:
table one : loanbook
table two : olb
staffno and loanstart columns are similar in both tables. For every staffno there can be multiple results in olb table.
PROBLEM:
I need to update loanstop column in table one (loanbook) with loanstop values from table two (olb) where staffno and loanstart are the same.
UPDATE loanbook3
SET loanbook3.loanstop = (
SELECT loanstop
FROM olb
WHERE olb.staffno = loanbook3.staffno
AND
olb.loanstart = loanbook3.loanstart
);
RESULT
#1242 - Subquery returns more than 1 row.
What do I do?
use limit 1 with your Subquery
UPDATE loanbook3 SET loanbook3.loanstop =
( SELECT loanstop FROM olb WHERE
olb.staffno = loanbook3.staffno
AND olb.loanstart = loanbook3.loanstart limit 1)
Related
I have 2 tables; alternate identifier table and a master table
the above image represents the column from alternate identifiers table
and master table have these columns
Now, First I fetch company_group_id from alternate identifier table where bank_entity_id='somevalue' and id_value = 'somevalue';
if this query return nothing than end. if this query return company_group_id than I will check that company_group_id in master table where company_group_id = selected company group id.
after getting this if its present in the table than i will check hashcode if it presents in the master table.
separate queries are like this:
select company_group_id from aes_batch.aes_company_group_alternate_identifiers
WHERE ID_VALUE = '525' and BANK_ENTITY_ID='UOBS';
select company_group_id, hashcode from aes_batch.aes_company_group_master where company_group_id = 'value from the last query' and hashcode="value";
I want to combine these queries to get same result.
this is what I tried but failed.
SELECT t1.company_group_id
FROM aes_batch.aes_company_group_alternate_identifiers t1
LEFT JOIN aes_batch.aes_company_group_master t2
ON t1.company_group_id = t2.company_group_id
WHERE t2.company_group_id IS NULL;
SELECT *
FROM aes_batch.aes_company_group_master B
WHERE NOT EXISTS (SELECT 1
FROM aes_batch.aes_company_group_alternate_identifiers A
WHERE B.company_group_id = A.company_group_id);
Select a.company_group_id, b.company_group_id,
b.hashcode, a.id_value
from aes_batch.aes_company_group_alternate_identifiers a
LEFT JOIN aes_batch.aes_company_group_master b
ON b.company_group_id = a.company_group_id
WHERE a.id_value in ('524','525')
and a.bank_entity_id='UOBS';
can someone help me in this?
Hi I am making a webrowser game and I am trying to get monsters into my data base when I get the error:
Subquery returns more then 1 row
here is my code
INSERT INTO monster_stats(monster_id,stat_id,value)
VALUES
( (SELECT id FROM monsters WHERE name = 'Necroborg!'),
(SELECT id FROM stats WHERE short_name = 'atk'),
2);
any ideas how to fix this problem?
Try use LIMIT 1
INSERT INTO monster_stats(monster_id,stat_id,value) VALUES ((SELECT id FROM monsters WHERE name = 'Necroborg!' LIMIT 1),(SELECT id FROM stats WHERE short_name = 'atk' LIMIT 1),2);
Or you could use Insert from select, with join, if you have relations with 2 tables.
INSERT INTO monster_stats(monster_id,stat_id,value)
(SELECT monsters.id, stats.id, 2 as value FROM monsters
LEFT JOIN stats on monsters.id = stats.monsters_id
WHERE monsters.name = 'Necroborg!'
AND stats.short_name = 'atk'
)
MYSQL insert from select:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
The problem is one or both of the following:
There is more than one monster named 'Necroborg!'.
There is more than on stat named 'atk'.
You need to decide what you want to do. One option (mentioned elsewhere) is to use limit 1 to get only one value from each statement.
A second option is to better specify the where clause so you get only one row from each table.
Another is to insert all combinations. You would do this with insert . . . select and a cross join:
INSERT INTO monster_stats(monster_id, stat_id, value)
SELECT m.id, s.id, 2
FROM (SELECT id FROM monsters WHERE name = 'Necroborg!') m CROSS JOIN
(SELECT id FROM stats WHERE short_name = 'atk');
A third possibility is that there is a field connecting the two tables, such as monster_id. But, based on the names of the tables, I don't think that is true.
I am trying to fill in fields in a table with date of another table.
In the table 'blanko' I have a column 'product_sku' and 'virtuemart_product_id'.
In the table 'jml_virtuemart_products' I have (among others) the columns 'product_sku' and 'virtuemart_product_id'.
Now I want to add values from jml_virtuemart_products.virtuemart_product_id column into the the same column in 'blanko' from rows with where product_sku is the same.
I am trying with this query and it works partialy.
UPDATE blanko b1 SET virtuemart_product_id = (SELECT virtuemart_product_id FROM jml_virtuemart_products v1 WHEREe v1.product_sku = b1.product_sku);
The problem is that it add endless amount of rows with NULL values.
Can someone explain what I am doing wrong? I am running in circles...
Better way is to use join to update the record
update blanko b1
join jml_virtuemart_products v1 on v1.product_sku = b1.product_sku
set b1.virtuemart_product_id = v1.virtuemart_product_id
i am stuck with a simple query if someone can help me on on the same.
i want to update one field with query from another table
the table structure is as follows:-
table stockmain - fields - itemcode, avgcost
table sales - fields - itemid, saleprice, costprice(this field is to be generated with query from stockmain table(avgcost field)
the query is follows:-
$qry = "UPDATE sales SET costprice = SELECT avgcost FROM stockmain WHERE itemcode = 'sales.itemid' ";
You can join these two tables to get one data set, and then copy data from one field to another, e.g. -
UPDATE sales sl
JOIN stockmain stm
ON stm.itemcode = sl.itemid
SET sl.costprice = stm.avgcost;
I have 2 tables: tbl_taxclasses, tbl_taxclasses_regions
This is a one to many relationship, where the main record ID is classid.
I have a column inside the first table called regionscount
So, I create a Tax Class, in table 1. Then I add regions/states in table 2, assigning the classid to each region.
I perform a SELECT statement to count the regions with that same classid, and then I perform an UPDATE statement on tbl_taxclasses with that number. I update the regionscount column.
This means I'm writing 2 queries. Which is fine, but I was wondering if there was a way to do a SELECT statement inside the UPDATE statement, like this:
UPDATE `tbl_taxclasses` SET `regionscount` = [SELECT COUNT(regionsid) FROM `tbl_taxclasses_regions` WHERE classid = 1] WHERE classid = 1
I'm reaching here, since I'm not sure how robust MySQL is, but I do have the latest version, as of today. (5.5.15)
You could use a non-correlated subquery to do the work for you:
UPDATE
tbl_taxclasses c
INNER JOIN (
SELECT
COUNT(regionsid) AS n
FROM
tbl_taxclasses_regions
GROUP BY
classid
) r USING(classid)
SET
c.regionscount = r.n
WHERE
c.classid = 1
Turns out I was actually guessing right.
This works:
UPDATE `tbl_taxclasses`
SET `regionscount` = (
SELECT COUNT(regionsid) AS `num`
FROM `tbl_taxclasses_regions`
WHERE classid = 1)
WHERE classid = 1 LIMIT 1
I just needed to replace my brackets [] with parenthesis ().