I have been reading for a few hours but my learning curve just isn't helping! I'm trying to find a few rows by doing a select statement, then when it matches, I need to grab the result and pair it up with another table then do an update. Somehow, from what I'm reading and applying, it's not helping me much.
Please kindly help me as I can't comprehend these things without seeing and applying what I'm doing... Here is my code:
select code as codea from routes where r1=1 (update plans set active=1 where code=codea) limit 100
You can update with JOIN like so:
UPDATE plans p
INNER JOIN routes r ON p.code = r.codea
SET p.active = 1
WHERE r.r1 = 1
LIMIT 100
Is this what you need?
update plans set
active = 1
where code = (select code as codea
from routes
where r1=1)
Related
I am currently doing some SQL magic and wanted to update the stock in my companies ERP program. However if I try to run the following query I get the error mentioned in the title.
update llx_product lp
set stock = (select sum(ps.reel)
from llx_product_stock as ps, llx_entrepot as w
where w.entity IN (1)
and w.rowid = ps.fk_entrepot
and ps.fk_product = lp.rowid
group by ps.rowid)
The subquery by itself returns just one row if used with a rowid for the product.
select sum(ps.reel)
from llx_product_stock as ps, llx_entrepot as w
where w.entity in (1)
and w.rowid = ps.fk_entrepot
and ps.fk_product = 7372
group by ps.rowid
Any help would be appreciated
I would suggest writing the query as:
update llx_product lp
set stock = (select sum(ps.reel)
from llx_product_stock ps join
llx_entrepot w
on ps.fk_product = lp.rowid
where w.entity in (1) and
w.rowid = ps.fk_entrepot
);
An aggregation query with no group by cannot return more than one row. It is unclear how your version is returning more than one row because the key used in the group by also has an equality comparison. Perhaps there is some type conversion issue at play.
But in any case, without the group by, you cannot get the error you are currently getting.
For anyone wondering the solution to my issue was a very simple query, Gordon pointed in the right direction and I made it harder than it should be.
update llx_product lp
left join llx_product_stock ps on lp.rowid = ps.fk_product
set lp.stock = ps.reel
I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?
I'm trying to write a query that takes a point, given to a player in a match event, multiply it by another column and then by 3, and then copy the result in the players table where the player already exists.
There is an error but I can't find what's wrong. Can you help me, please, with solving this issue?
UPDATE
j7yh8_bl_match_events,
j7yh8_bl_players
SET
j7yh8_bl_players.player_points = SELECT COUNT(j7yh8_bl_match_events.e_id) * j7yh8_bl_match_events.ecount * 3,
WHERE
j7yh8_bl_match_events.player_id = j7yh8_bl_players.id AND j7yh8_bl_match_events.e_id = 5;
Any help will be appreciated.
Thank you.
In MySQL, you can use a join with update. Any aggregation functions, though, have to be in subqueries.
I'm not 100% sure that the expression is for calculating points. It seems that something like this solves your problem:
UPDATE j7yh8_bl_players p LEFT JOIN
(SELECT me.player_id, SUM(me.ecount) * 3 as points
FROM j7yh8_bl_match_events me
WHERE me.e_id = 5
GROUP BY me.player_id
) me
ON me.player_id = p.id
SET p.player_points = me.points
i have a little problem. I use phpMyAdmin.
Thats my try:
SELECT AVG (ws_spiel_berechnung.note) as Test
FROM ws_spiel_berechnung
LEFT JOIN ws_spieler
ON ws_spiel_berechnung.spieler_id = ws_spieler.id
INNER JOIN ws_spiel ON ws_spiel.id = ws_spiel_berechnung.spiel_id
where ws_spiel.spieltyp = 'Ligaspiel' and ws_spieler.id = 6875;
update ws_spieler set note_anpassung = Test where id = 6875
I want to take the AVG(ws_spiel_berechnung.note) to ws_spieler.note_anpassung. For example the result from SELECT AVG is 2,46. Now 2,46 should be updated to note_anpassung. But this doesn't work (i think because of the "Test").
Another question: The id is just an example. I want this for every id (it beginns at id 1 und ends at id 1000). The target is that i have the AVG note from every id at "ws_spieler.note_anpassung".
I use google and this site, but i don't understand it. I would be happy if you can help me. And sorry for my english ;)
In this case, I think you can do what you want with a correlated subquery:
update ws_spieler s
set s.note_anpassung = (select avg(sp.note)
from ws_spiel_berechnung sp
where sp. spieler_id = s.id
)
where s.spieltyp = 'Ligaspiel' and s.id = 6875;
I'm messing around with a mysql query over 3 tables and and I just can't get it work.
The situation: I have 3 tables.
Now I try to make a mysql query based on an inputfield, where I put in a "oxid" from the table "oxarticles" and the result should be, that I get all the articles from the category where the article/oxid is which I put in the inputfield.
Example: I put "oxid" 2 in a inputfield and press submit and the result should looks like this:
Lenkrad
Reifen
Sitz
I tried a lot but never come close to. I made a day before another query that show me all the categories based on a article but I cant modified that and used it for my actual problem.
I hope you can help me :)
Thats what I get so far, but I think this is not even close cause I get a white page.
$result = mysql_query("SELECT DISTINCT oxtitle FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatnid IN (SELECT oxcatnid FROM oxobject2category WHERE oxobjectid = 2)")
or die(mysql_error()); ;
Now i got it:
The follow querys work:
SELECT DISTINCT oxtitle FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatind IN (SELECT oxcatind FROM oxobject2category WHERE oxobjectid = 2
and this one also:
select distinct
a.oxtitle
from
oxarticles a,
oxobject2category oc
where
a.oxid = oc.oxobjectid and
oxcatind in (select oxcatind from oxobject2category where oxobjectid=2
Thank you for the help :)