MySQL multi query in one - mysql

I have a table name user with user_id, image_id .... (and some others attribute)
Another table name image with image_id and image_name ...
I wanna search an image_id by image_name, and update to user table that image_id found
like this:
UPDATE person SET image_id = (
SELECT image_id
FROM image
WHERE image.src='02.jpg' ) WHERE person_id=2
Of course, it doesn't work. I just use that query to show you what i want.
I can use 2 queries on php code, the first get image_id, the second update it to table.
But I think there is a better way to do it.

The problem here might well be that the subselect is returning more than one row and you are trying to update only one row in the table person.
Try that:
UPDATE person SET image_id = ( SELECT distinct image_id FROM image WHERE image.src='02.jpg' ) WHERE person_id=2
Can we see the error?

I think its not right way to use the sub-query in main query, you can create trigger in MySQL database or if you would like to use by this way then:
UPDATE person
SET person.image_id = ( SELECT image_id
FROM image
WHERE image_name='02.jpg'
)
WHERE person.person_id=2

update person p set p.image_id=(select i.image_id from images i where i.src='02.jpg' limit 1 ) where p.person_id=2
step 1: use table alias ( p, i ) so your fieldnames are not ambiguously
step 2: use limit 1 on subselect -> get not more than 1 rows
Better solution is to use some logic on serverside (php/perl...) and work with two seperate querys
Hope it helps...
Thomas

Related

MySQL: One statement in one table

I need help :( I have table like this...
ID|code|item|user|
01|aaaa|1111|0001|
02|bbbb|1111|0001|
03|cccc|1111|0001|
04|dddd|1111|0001|
05|aaaa|1111|0002|
06|eeee|1111|0002|
07|ffff|1111|0001|
I'm user 0002 and I know my item numer (for example 1111). I don't know other users, ids and other codes, but i have to get only 02,03,04,07 results (for this example). Any sinle and duplicated rows (for code column) with user 002 should be ignored... if you know what i mean. Any ideas how? :(
This can be done with a subquery filter
select *
from myTable
where code not in (
select code
from myTable
where user = '0002'
)
Try with:
select ID
from myTable
where user <> '0002'
group by ID, code
having count(code) = 1

UPDATE MySQL query error "Subquery returns more than 1 row"

I'm trying to achieve this query but I got an error:
UPDATE ps_product_lang
SET name=(select name from ps_product_lang_backup where id_lang=2)
WHERE id_lang = 3
But I got the Subquery returns more than 1 row
Probably I must use JOIN but I'm really new to MySQL and cannot do myself.
What i'm trying to do is simple: i have in my database 3 languages, wish to copy data from one language (english id_lang 2) to paste in another (russian, id_lang 3)
I assume the table also has a product_id column that is unique for a product.
You need to tell the database to pick the english name for the same product.
UPDATE ps_product_lang
SET name=(select name from ps_product_lang_backup
where id_lang=2
and ps_product_lang.product_id = ps_product_lang_backup.product_id)
WHERE id_lang = 3
I'll try my best to answer this
after looking to your query it is clear that (select name from ps_product_lang_backup where id_lang=2) has more then one result that's why it's causing you the error. Now, to fix this you can do two things.
Either you can delete one record with the same id_lang(easy one).
OR, you can change your subquery like this:
select name from ps_product_lang_backup where id_lang=2 LIMIT 1
which will change your query like this:
UPDATE ps_product_lang SET name=(select name from ps_product_lang_backup where id_lang=2 LIMIT 1) WHERE id_lang = 3
Hope this help
Here it means that your SELECT Query Return more than two results Try this
SET name=(select name from ps_product_lang_backup where id_lang=2 LIMIT 1)

How do I update a table that is being queried within the query itself?

I'm trying to update a table through a query, the query itself is created using the results of queries to other tables (one of which is the table we are updating)...
However, I keep getting an error when I try to execute the query... After some research I found out I have to encase the inner query with a SELECT * FROM ()... but this doesn't seem to have worked...
I can't figure out how to bypass this error with my MySQL Query...
This is the error I'm getting...
[Err] 1093 - You can't specify target table 'players' for update in
FROM clause
This is my query...
DELETE FROM players WHERE name='Henry' AND player_group_id IN
(
SELECT id FROM playergroups WHERE player_set_id=
(
SELECT id FROM playersets WHERE player_name=
(
SELECT name FROM
(
SELECT name FROM players WHERE player_group_id=
(
SELECT id FROM playergroups WHERE player_set_id=
(
SELECT id FROM playersets WHERE player_name='Henry'
)
)
) AS P1
)
)
);
use joins, avoid to much use of nested statements
You should really find a better way to navigate though the tables, but where exists is a good way to around that error. Or you could stage off the data and delete based of PIs.
DELETE FROM players A
WHERE name = 'Henry'
AND WHERE EXISTS
(
SELECT 1
FROM playersgroup B
WHERE A.players_group_id = B.players_group_id
)
ETC.. ETC.. Depending on how many tables you need to reference.

MySQL INSERT INTO table 1 SELECT table 2 with different column name

I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'

Get a list of ids not present in a table

I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.