SQL join and get another column - mysql

I'm trying to join two tables so I can easily order it, as one contains the names of items and the other doesn't. The tables:
user_games: UGID, UID, APPID, playtime
games_steam: APPID, name
my SQL so far:
SELECT user_games.*
FROM user_games
JOIN games_steam ON user_games.APPID = games_steam.APPID
WHERE user_games.UID = '76561197996836099'
GROUP BY user_games.APPID
ORDER BY games_steam.name ASC
LIMIT 0, 30
Just no idea how to get the name column into this aswell.

Is this what you are looking for?
SELECT user_games.*, games_steam.name
FROM user_games
JOIN games_steam
ON user_games.APPID = games_steam.APPID
WHERE user_games.UID = '76561197996836099'
GROUP BY user_games.APPID
ORDER BY games_steam.name ASC
LIMIT 0, 30

Related

Get last row in LEFT JOIN

What I am trying to do it with below code, getting all keywords with their positions via LEFT JOIN, it works fine but it shows the first position of each keyword, but I want to show the last position that recorded (by date).
SELECT keyword.id, keyword.title, keyword.date, rank.position FROM keyword
LEFT JOIN rank
ON rank.wordid = keyword.id
GROUP BY keyword.id
ORDER BY keyword.date DESC
How can I do this? Should I use subquery or what? Is there any way to do this without a subquery?
SAMPLE DATA
What I want:
Get 17 instead of 13, I mean last record of position.
Do not use group by for this! You want to filter, so use a where clause. In this case, using a correlated subquery works well:
SELECT k.id, k.title, k.date, r.position
FROM keyword k LEFT JOIN
rank r
ON r.wordid = k.id AND
r.date = (SELECT MAX(r2.date)
FROM rank r2
WHERE r2.wordid = k.id
)
ORDER BY k.date DESC
You can use below query
SELECT keyword.id, keyword.title, keyword.date, rankNew.position FROM keyword LEFT JOIN (
SELECT rank.wordid, rank.position FROM rank ORDER BY rank.id DESC LIMIT 0, 1) AS rankNew ON (rankNew.wordid = keyword.id);
You can get more reference from Retrieving the last record in each group - MySQL

How to display only the multiple maximal values of my result table?

Using this MySQL queries on my database:
SELECT movie.name, SUM(heroes.likes) AS 'success'
FROM heroebymovie JOIN
heroes
ON heroes.ID = heroebymovie.heroID JOIN
movie
ON movie.ID = heroebymovie.movieID
GROUP BY movie.ID
ORDER BY SUM(heroes.likes) DESC
I obtain this result:
|name |success |
|Avengers 2 |72317559 |
|Avengers |72317559 |
|Captain America : Civil War|67066832 |
I would like to display only the movies with the highest number of “success” (in this case “Avengers 2” and “Avengers”).
Can someone explain the way of doing it?
A simple way is using an having clause that filter for the max value ( in this case the ordered list of sum desc limit 1)
SELECT movie.name, SUM(heroes.likes) AS success
FROM heroebymovie JOIN heroes ON heroes.ID = heroebymovie.heroID
JOIN movie ON movie.ID = heroebymovie.movieID
GROUP BY movie.ID
HAVING success = (
SELECT SUM(heroes.likes)
FROM heroebymovie JOIN heroes ON heroes.ID = heroebymovie.heroID
JOIN movie ON movie.ID = heroebymovie.movieID
GROUP BY movie.ID
ORDER BY SUM(heroes.likes) DESC
LIMIT 1
)
ORDER BY SUM(heroes.likes) DESC
You are looking for a limit, but want to consider ties. MySQL supports the LIMIT clause, but unfortunately no accompanying ties expression.
In standard SQL you would simply add
FETCH 1 ROW WITH TIES;
and be done with it. (SQL Server does the same with TOP(1) WITH TIES.)
Another way would be to use standard SQL's MAX OVER: MAX(SUM(heroes.likes)) OVER() and only keep rows where the sum matches the maximum. Or use RANK OVER. But again, MySQL doesn't support either of these.
So your main option is to execute the query twice, like in this pseudo code:
select sum ... having sum = (select max(sum) ...)
An easy way to get the max of the sums in MySQL is to order by sums descending and limit the results to one row.
SELECT m.name, SUM(h.likes) AS "success"
FROM heroebymovie hm
JOIN heroes h ON h.ID = hm.heroID
JOIN movie m ON m.ID = hm.movieID
GROUP BY m.ID
HAVING SUM(h.likes) =
(
SELECT SUM(h2.likes)
FROM heroebymovie hm2
JOIN heroes h2 ON h2.ID = hm2.heroID
GROUP BY hm2.movieID
ORDER BY SUM(h2.likes) DESC
LIMIT 1
);

MySQL with JOIN-Query

I want to connect differend Queries with each other, but I can't figure out a way how...
Table mcr_versions contains a list of plugin versions, I want to get the plugin_id of the last 10 updates:
SELECT plugin_id FROM mcr_versions ORDER BY time DESC LIMIT 10
At the next step I need to get the plugin from another table:
SELECT id, name, logo, rating FROM mcr_plugins WHERE id = plugin_id
Now I have a table containing plugin-category-relationships, only with plugin_id and cat_id, I want to get one cat_id for each plugin:
SELECT cat_id FROM mcr_plugin_cat WHERE plugin_id = id
The last table contains the name of the category:
SELECT name FROM mcr_categories WHERE id = cat_id
How can I connect these queries to one query?
select mcr_categories.name from mcr_versions
left join mcr_plugins on mcr_plugins.id = mcr_versions.plugin_id
left join plugin-category-relationships on plugin-category-relationships.plugin_id = mcr_versions.plugin_id
left join mcr_categories on mcr_categories.cat_id = plugin-category-relationships.cat_id
order by mcr_versions.time desc limit 10;
i may be wrong at some of the tables , but i beleive u got my point

Dynamic SQL with embedded table

I have a query which does the job but instead of the trans_inventory (which is an ID of the location) I need to get the location_name.
This one is working to get the id
SELECT *
FROM {TABLE}
WHERE trans_product = 646
ORDER BY trans_date2 DESC Limit 1
But I wonder if I can do it this way, somehow embed the location table, I have tried but below doesn't work
SELECT *, site_location.location_name
FROM site_trans
cross join
(select *
From site_location)
site_location
WHERE trans_product=646 ORDER BY trans_date2 DESC Limit 1
sl.location in the JOIN must be the site_location location id field name - I used locastion_id but it might be likely id as well. I used LEFT JOIN to avoid missing site_trans in case there is no matching location id.
SELECT s.*, sl.location_name
FROM site_trans AS s
LEFT JOIN site_location AS sl ON sl.location_id = s.location_id
WHERE s.trans_product=646
ORDER BY s.trans_date2 DESC
Limit 1

retrive all data with counting per item when joining two table

I have two tables:
Table 1: contest_video
Table Rows: cv_id, uid, cid, cn_id, video_name, video_detail, video, image, cverify, date
Table 2: contest_vote
Table Rows: vid, cv_id, voter_id, date
Now I want to join these two table that will with a counting. Like total number of vote (vid) of a video(video name)
I have tried this way:
SELECT *
FROM (`contest_video`)
LEFT JOIN `system_user`
ON `system_user`.`id` = `contest_video`.`uid`
LEFT JOIN `contest_vote`
ON `contest_vote`.`cv_id` = `contest_video`.`cv_id`
WHERE `contest_video`.`cid` = '1'
ORDER BY `contest_video`.`created_date` DESC
But it returns only data without counting. Need experts help please.
You can do it like this
use group by and column names instead of *
SELECT
cv_id,
uid,
cid,
cn_id,
video_name,
video_detail,
video,
image,
cverify,
date,
count(`contest_video`.`cv_id`) Total
FROM (`contest_video`)
LEFT JOIN `system_user`
ON `system_user`.`id` = `contest_video`.`uid`
LEFT JOIN `contest_vote`
ON `contest_vote`.`cv_id` = `contest_video`.`cv_id`
WHERE `contest_video`.`cid` = '1'
GROUP BY `contest_video`.`video_name`
ORDER BY `contest_video`.`created_date` DESC