Wrong syntax in a SQL query - mysql

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

Related

Subquery returns more than one row error when one row is returned

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

MySQL find duplicates based on some matching fields and some not

I am trying to create a MySQL query of game discs to find some duplicates based on some values being the same and some not.
I need the fields 'name', 'disc', 'platform', 'region' to be the same.
But I also need the field 'version' to be different.
I have already tried a number of queries to do this such as the one below but none seem to work as desired.
SELECT *
FROM media.amps_2000_box_a
INNER JOIN (SELECT *
FROM media.amps_2000_box_a
GROUP BY name
HAVING COUNT(name) > 1) dup
ON media.amps_2000_box_a.name = dup.name and media.amps_2000_box_a.disk = dup.disk and media.amps_2000_box_a.format = dup.format and media.amps_2000_box_a.region = dup.region and media.amps_2000_box_a.version<> dup.version
order by dup.name;
Would anyone be able to help me fix this query?
Thanks in advance.
slick
Maybe I'm missing something but the solution seems quite trivial:
SELECT DISTINCT
amps1.name,
amps1.disk,
amps1.platform,
amps1.region
FROM media.amps_2000_box_a amps1
JOIN media.amps_2000_box_a amps2
ON amps1.name = amps2.name
AND amps1.disk = amps2.disk
AND amps1.platform = amps2.platform
AND amps1.region = amps2.region
AND amps1.version <> amps2.version
ORDER BY amps1.name;

Error Code 1242: Subquery retuens more than 1 row

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?

Mysql Select with select result in an other table

I need some help, I don't know correct term of what im trying to do so im not able to find any example of what im trying to acheive.
Im hopping someone on here could just point me how how i could do this simple query and il be able to create one on my own.
SELECT * FROM
`product_views` pv,`product_sales` ps
WHERE
pv.`SessionId` = ps.`SessionId` {result of} ps.`ProductId` = 1
The syntax im trying to figure out is basacly how to get result of a query in a where clause.
Thanks!
Edit.
I realize that the fact that I don't know correct terms of what im trying to achieve is confusing so il show how in this point of time I would get the same result but in two query
SELECT ps.`SessionId` FROM `product_sales` ps WHERE ps.`ProductId` = 1;
Then with that the result of that query I would have let say sessionId 20 ( to keep it simple )
And then i would have to re-query with
SELECT * FROM `product_views` pv WHERE pv.SessionId = {ValueOfLastQuery};
A join would help you achieve what you're looking for. Here's a great introduction.
Basically, you'd want to do something like:
SELECT * FROM
product_views
LEFT JOIN product_sales
ON product_views.SessionId = product_sales.SessionId
WHERE product_sales.ProductId = '1'
Edit:
I think you could also use a subquery, which would look something like:
SELECT * FROM
product_views
WHERE SessionId = (SELECT SessionId from product_sales WHERE ProductId = '1')
How about a subquery:
SELECT * FROM `product_views` pv
WHERE pv.SessionId IN
(SELECT ps.`SessionId` FROM `product_sales` ps WHERE ps.`ProductId` = 1)

mysql select then update / joining?

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)