From MYSQL SELECT to a MYSQL VIEW - mysql

I don't know how to make this.
Background:
Profiles -> table with user profiles
Score -> table with users' score (One user can have multiple scores) (This table has a created_at field to know when I introduce the new score)
Imagine that score table save klout score data.
How do convert this query to a view?
SELECT t1.id,
t1.name,
t1.screen_name,
t1.description,
t1.url,
t1.statuses_count,
t1.followers_count,
t1.friends_count,
t1.listed_count,
t1.favourites_count,
t1.utc_offset,
t1.time_zone,
t1.verified,
t1.lang,
t1.profile_image_url,
t1.geo_enabled,
t1.location,
t1.lat,
t1.lng,
t1.created_at,
t3.score,
t3.delta,
t3.detail
FROM profiles t1 LEFT JOIN (SELECT t2.user_id, t2.score, t2.delta, t2.detail
FROM scores t2
ORDER BY t2.created_at DESC)
AS t3 ON t3.user_id = t1.id
GROUP BY t1.id

The problem VIEW cannot contain subquery, the subquery in your query can be directly join on the table since you haven't perform any aggregation.
CREATE VIEW viewName
AS
SELECT t1.id,
t1.name,
t1.screen_name,
t1.description,
t1.url,
t1.statuses_count,
t1.followers_count,
t1.friends_count,
t1.listed_count,
t1.favourites_count,
t1.utc_offset,
t1.time_zone,
t1.verified,
t1.lang,
t1.profile_image_url,
t1.geo_enabled,
t1.location,
t1.lat,
t1.lng,
t1.created_at,
t3.score,
t3.delta,
t3.detail
FROM profiles t1
LEFT JOIN scores t3
ON t3.user_id = t1.id
-- GROUP BY t1.id
I can't get why you have a GROUP BY clause in your query.

You can split your code into 2 views
View 1:
CREATE VIEW step1
AS
SELECT t2.user_id, t2.score, t2.delta, t2.detail
FROM scores t2
ORDER BY t2.created_at DESC
View 2:
CREATE VIEW step2
AS
SELECT t1.id,
t1.name,
t1.screen_name,
t1.description,
t1.url,
t1.statuses_count,
t1.followers_count,
t1.friends_count,
t1.listed_count,
t1.favourites_count,
t1.utc_offset,
t1.time_zone,
t1.verified,
t1.lang,
t1.profile_image_url,
t1.geo_enabled,
t1.location,
t1.lat,
t1.lng,
t1.created_at,
t3.score,
t3.delta,
t3.detail
FROM profiles t1
LEFT JOIN step1 AS t3 ON t3.user_id = t1.id
GROUP BY t1.id
I am not sure why you are grouping by t1.id, but maybe there's something in your data that I don't know about

Related

select all row except those specified in the subquery

I make a selection on request and it behaves correctly.
SELECT t1.id, MIN(t1.rate)
FROM offers AS t1
WHERE t1.stock > 0
GROUP BY t1.ean;
AS soon as I nest it in another query, then the difficulty arises that WHERE NOT IN expects one column, but I need to keep MIN for GROUP BY to be preserved.
Here is query:
SELECT t2.id, t2.name, t2.ean
FROM offers AS t2
WHERE t2.id NOT IN (SELECT t1.id, MIN(t1.rate)
FROM offers AS t1
WHERE t1.stock > 0
GROUP BY t1.ean);
So here is answer:
SELECT t1.id
FROM offers AS t1
WHERE t1.id NOT IN (
SELECT f.id
FROM (SELECT
ean,
Min(net_price) AS minprice,
stock
FROM offers
GROUP BY ean
)
AS x INNER JOIN offers
AS f ON f.ean = x.ean AND f.net_price = x.minprice);

Join 4 Tables to find total and sort by total

i have 3 tables
1) users
2) user_likes
3) user_pictures
4) user_pucture_likes
users(id, name)
user_likes(id, user_id, like)
user_pictures(id, user_id, filename)
user_picture_likes(id, user_picture_id, like)
what i am trying to do is to sort the user on the total like on user_like + user_picture_like
I am not good with joining more than 2 tables.
i got upto here
SELECT t2.user_id, sum(t1.total_likes) as image_likes from user_pictures as t2 JOIN (
SELECT sum(like_status) as total_likes, user_picture_id FROM `user_picture_likes` GROUP BY user_picture_id ORDER BY total_likes DESC
)as t1
ON t1.user_picture_id = t2.id GROUP BY t2.user_id ORDER BY image_likes DESC
and how do i proceed from here?
Try this
SELECT T1.Id AS UserID, SUM(T2.like), Tmp.filename AS Image, Tmp.Imageslikes
FROM users T1
LEFT JOIN user_likes T2 ON T1.Id = T2.user_id
LEFT JOIN (
SELECT T3.user_id, T3.filename, SUM(T4.likes) Imageslikes
FROM user_pictures LEFT JOIN user_picture_likes T4 ON T3.Id = T4.user_picture_id
GROUP BY T3.user_id, T3.filename
)Tmp ON T1.Id = Tmp.user_id

MySQL Delete using subquery on same table

Using the following query, I am able to return the results I need.
SELECT t1.*
FROM lms_attendance t1
WHERE t1.id = (SELECT t2.id
FROM lms_attendance t2
WHERE t2.user = t1.user
ORDER BY t2.id DESC
LIMIT 1)
However, when trying to do a DELETE instead, this does not work. Through trial and error, I either get unknown column or can't specify target updated in from clause. The main issue here is that I absolutely have to have the WHERE clause as it is so that it returns the maximum id for each user, and not for the entire table.
Try this query below. Please run a select first to confirm if it is the correct records before running the delete. Better if you keep a backup table too.
Run select:
SELECT t1.* FROM lms_attendance t1
LEFT JOIN (SELECT user, MAX(id) id
FROM lms_attendance
GROUP BY user) t2
ON t1.id = t2.id
WHERE t2.id IS NULL
If looks OK;
DELETE t1.* FROM lms_attendance t1
LEFT JOIN (SELECT user, MAX(id) id
FROM lms_attendance
GROUP BY user) t2
ON t1.id = t2.id
WHERE t2.id IS NULL

Issues with SQL queries

I have 2 tables and result as shown in the image below: MySQL DB
What would be best way to join the two tables so we get the result as shown above.
SELECT * FROM (SELECT id, desc FROM table2) as T1
LEFT JOIN (SELECT * FROM table1) as T2 ON T1.id = T2.id
I guess my SQL is not working.
You can use a LEFT JOIN with COALESCE:
SELECT t1.id, COALESCE(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
Use a left join with coalesce to prioritize table 2's values if they are present, but fallback on table 1's values if not.
select t1.id,
coalesce(t2.desc, t1.desc) as desc,
t1.d1, t1.d2
from table1 t1
left join table2 t2
on t2.id = t1.id
order by t1.id
You can use ifnull:
SELECT t1.id, ifnull(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
coalesce or case .. when is also possible. All together with the left join

Inner Join with Count

I am trying to get the soul count of a player in the game as well as all their other stats. The query I am trying is
SELECT t1.*, (SELECT COUNT(*) FROM t2 GROUP BY SoulLocationName WHERE SoulLocationName=t1.Name) AS SoulCount
FROM tblAvatar t1
JOIN tblAvatar t2 ON t1.Name = t2.SoulLocationName
Where am I going wrong?
Try this query
SELECT t1.*, t2.SoulCount
FROM tblAvatar t1
JOIN(SELECT SoulLocationName,COUNT(*) as SoulCount FROM tblAvatar GROUP BY SoulLocationName) t2 ON t1.Name = t2.SoulLocationName
SELECT t1.*,
COUNT(t2.SoulLocationName) as SoulCount
FROM tblAvatar t1
JOIN tblAvatar t2
ON t1.Name = t2.SoulLocationName