Mysql Select with select result in an other table - mysql

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)

Related

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;

Wrong syntax in a SQL query

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

mysql query 3 tables

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 :)

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)

Nested MySQL query?

I have this MySQL statement which works fine:
SELECT claim_items.item_id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
However what I want to do is to get the sum of the per_item field generated in the MySQL statement. Is it possible to do a nested statement to do that? I know I could create a view and then do it, but would prefer to do it in one statement.
Thanks for your help, much appreciated!
select t.id, SUM(t.per_item)
from
(
SELECT claim_items.item_id as id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
) t
group by t.id
Hope this should help.
If you just want the sum over all rows you can do it like that (using explicit joins for better readability):
SELECT SUM( claim_items.quantity * items_rate.rate )
FROM claim_items
JOIN items_rate ON ( items_rate.items_id = claim_items.item_id )
WHERE claim_items.claim_id = 1