I have a table (user) with the following info:
id | user_id | name | country | referral_id
----------------------------------------------------
1 | 10 | Jhon | India | 0
2 | 11 | Krish | America | 0
3 | 12 | Boss | Canada | 0
4 | 13 | Jack | India | 11
5 | 14 | Ronald | Japan | 10
6 | 15 | Jey | Germany | 10
And have other table (total_earning) with following info:
id | user_id | date | earning
--------------------------------------------
1 | 10 | 2015-10-25 | 4.4$
2 | 14 | 2015-10-25 | 2.2$
3 | 15 | 2015-10-27 | 3.0$
4 | 15 | 2015-10-25 | 1.5$
I Want to give Referral Payment (10% of that user earning) to users.
eg. User.10 has 2 referrals (User.14 and User.15), so i want give 10% from user.14 & user.15 earning.
How can I create a MySQL View table from this 2 table?
You need a two-part query. The first query is on a per-user basis of ANY possible earnings. This is the "JustMe" alias... what did EACH person earn directly.
From that, doing a LEFT-JOIN (as not all users had referrals from other) to the second query "Referrals" alias. This joins the earnings to the users table and grabs the referral Id from the USERS table as the grouping. You could have 10 people all be referral from user X, so you want ALL their earnings rolled-up into one for user X.
From that, then JOIN to the user table to pull the who the main person was with their earnings PLUS a column to show the total referral bonus they would receive.
select
U.Name,
U.Country,
JustMe.JustMyEarnings,
coalesce( Referrals.ReferralEarnings * .1, 0 ) as ReferralBonus
from
( select
te.user_id,
sum( te.earning ) justMyEarnings
from
total_earning te
group by
te.user_id ) JustMe
LEFT JOIN
( select
U2.referral_id,
sum( te2.earning ) as ReferralEarnings
from
total_earnings te2
join users U2
on te2.user_id = U2.user_id
AND U2.referral_id > 0
group by
U2.referral_id ) Referrals
on JustMe.user_id = Referrals.referral_id
JOIN Users U
on JustMe.user_id = U.user_id
AND, if you want this as a view, just
CREATE VIEW UserReferralView as
(entire select from above...)
BUT, if you want the results for a single user, then I would adjust as you DO NOT want the inner queries to query the entire table every time you want totals for one person. I would adjust the inner queries to something like
( select
te.user_id,
sum( te.earning ) justMyEarnings
from
total_earning te
where
te.user_id = THE_ONE_USERID_YOU_WANT
group by
te.user_id ) JustMe
LEFT JOIN
( select
U2.referral_id,
sum( te2.earning ) as ReferralEarnings
from
total_earnings te2
join users U2
on te2.user_id = U2.user_id
AND U2.referral_id > 0
AND te2.referral_id = THE_ONE_USERID_YOU_WANT
group by
U2.referral_id ) Referrals
Related
I have 2 tables
Vendor
ID | userid| address | Country
1 | 10 | NY | US
2 | 20 | Mumbai | INDIA
events_todo
ID | events_id| vendor| status
1 | 1 | 10 | Completed
2 | 2 | 20 | Inprogress
So I want to join these 2 tables and get all vendor table data. I want to join on the basis of if userid of vendor table exists in event_todo table's vendor. I also want to show of column of Count if the status is completed. For example:
ID | userid | address | Country | events_id | status | Count
1 | 10 | NY | US | 1 | Completed | 1
2 | 20 | Mumbai | INDIA | 2 | Inprogress| 0
I have applied the following query and I am getting results but not able to get the count of event status
SELECT `vendors`.`id`, `vendors`.`userid`, `vendors`.`address`,`vendors`.`country` AS `updatedAt`, `vendors`.`userid` IN (SELECT sum(events_todo.status) AS completed FROM events_todo ) AS `completed`, `events_todo`.`id` AS `events_todo.id`, `events_todo`.`events_id` AS `events_todo.events_id`, `events_todo`.`category` `events_todo.vendor`, `events_todo`.`created_by` `events_todo.status` FROM `vendors` AS `vendors` LEFT OUTER JOIN `events_todo` AS `events_todo` ON `vendors`.`userid` = `events_todo`.`vendor` WHERE (`vendors`.`city` LIKE '%%' AND `vendors`.`state` LIKE '%%' AND `vendors`.`country` LIKE '%%') AND events_todo.status IS NOT NULL
I am getting a Count of 0 for Status seems like the sum is not working properly. Please suggest what needs to be done.
You could go with a simple query like this
SELECT v.id, v.user_id, v.address, v.country, e.events_id, e.status, IF(e.status = 'Completed', 1, 0) AS count
FROM vendors AS v
LEFT JOIN events_todo AS e ON e.vendor = v.user_id
Output
id
user_id
address
country
events_id
status
count
1
10
NY
US
1
Completed
1
2
20
Mumbai
INDIA
2
Inprogress
0
I have two tables;
Users
id | username | club
-----------------------
1 | James5 | 2
2 | 007 | 1
3 | xmen | 2
4 | terminator | 2
suggestedusers
id | username | club
----------------------
1 | mark | 2
2 | bon | 1
3 | hero | 2
4 | scorpio | 2
5 | lame | 5
How do I join these tables to get the total of the clubs? e.g an answer like
club | clubCount
-------------------
2 | 6
1 | 2
5 | 1
I was thinking of the following query;
SELECT User.club, COUNT(User.club) + COUNT(suggestedusers.club) AS clubCount FROM User, suggestedusers
GROUP BY User.club
ORDER BY clubCount DESC
But the above script is not working.
Your target result is not clear to me.
You can try with this one
Select club, count(club) as clubcount from(
select users.id, users.username, users.club from users
UNION
select suggestedusers.id, suggestedusers.username,
suggestedusers.club from suggested users ) group by club, clubcount Order by
clubcount desc;
Assuming what you want is to get the number of users per club for both tables, this should work for you:
SELECT DISTINCT COALESCE(u.club, su.club) AS club
, COALESCE(count(u.club), count(su.club)) AS clubcount
FROM Users u
INNER JOIN Suggestedusers su ON su.club = u.club
GROUP BY u.club,su.club
ORDER BY clubcount DESC
Please check the names of tables and columns when using in your code.
I have question about mysql queries. The story goes something like this: I have table in which I store information about college trips. This table has attributes about name of a trip, id of a trip and activity. Activity can be 0 or 1, depending if trip is still active (1) or inactive (0). In second table I have information about students that have applied for trips with attributes: id, name, surname and id of a trip that student have applied for. I don't know mysql query that will show me only students that have applied for trips that are still active (acitivity=1).
For example let's have a look at these tables:
TRIPS
id | trip | activity
---+----------+-----------
1 | Paris | 0
2 | London | 1
3 | Belgrade | 0
4 | Prague | 1
STUDENTS
id | name | id_trip
---+----------+-----------
1 | Mark | 3
2 | Ana | 1
3 | Tom | 2
4 | Maya | 3
5 | Rachel | 4
6 | John | 2
RESULT
id | name | id_trip | trip | activity
---+----------+---------+---------+---------
3 | Tom | 2 | London | 1
5 | Rachel | 4 | Prague | 1
6 | John | 2 | London | 1
SELECT
s.id,
s.name,
s.id_trip,
t.trip,
t.activity
FROM
STUDENTS AS s
INNER JOIN TRIPS AS t ON ( t.id = s.id_trip )
WHERE
t.id = 1
hope this will work.
Try this:
SELECT s.id, s.name, s.id_trip, t.name, t.activity
FROM students s
JOIN trips t
ON s.id_trip = t.id
WHERE t.activity = 1
select * from students s
join trips t
on s.id_trip = t.id
where t. activity =1
Try this it may solve your problem:
select s.id as id, s.name as name,t.trip as trip,
t.activity as activity from trips t
join students s on
t.id = s.id_trip
where t.activity = 1
I Want to show accounts with their highest rank characters and SUM of Their Time Where stats on table accounts is 1 with just in one Query.
I have Two Table with below data :
Table: Accounts:
Id| Username |Stats
1 | player1 |1
2 | goodman |1
3 | goodbat |1
4 | ashasdd |0
Table: Characters:
Guid| Account | Name | Rank | Time |
213 | 1 | fres | 2 | 51 |
214 | 2 | sdg2 | 3 | 12 |
215 | 2 | fgax | 4 | 99 |
216 | 3 | zFvx | 8 | 23 |
217 | 3 | Sgzs | 2 | 13 |
Output/Result: (Show Accounts characters with their Highest rank character and Their Sum of time)
Username : player1 | Name: fres(Rank:2) |Time : 51
Username : goodman | Name: fgax(Rank:4) |Time : 111
Username : goodbat | Name: zFvx(Rank:8) |Time : 36
what's the simple MySQL Query ?
My bad Query: (don't work)
SELECT a.username, a.email, c.name, SUM(c.time), c.rank
FROM `auth`.`account` a, `characters`.`characters` c
WHERE a.id=c.account
ORDER BY c.rank ASC
LIMIT 20
should be enough.. i think.
SELECT c.Account, a.Username, c.Name, MAX(c.Rank) as maxrank, SUM(c.TIME) as sumtime FROM characters c LEFT JOIN Accounts a ON a.Id=c.Account GROUP By c.Account;
Something like this might work
SELECT username, name, d.rank, c.Time
FROM (
SELECT a.Username, b.account, MAX( b.Rank ) AS rank, SUM( b.Time ) AS TIME
FROM accounts a
INNER JOIN characters b ON a.ID = b.Account
WHERE a.stats =1
GROUP BY a.Username
) AS c
JOIN characters d ON d.account = c.account
AND d.rank = c.rank
ORDER BY rank
Not sure how to do it in just one select tho since then you might get the wrong name, thats not registered with the max rank.
Hi there coders around the world,
I'm working on a project where users can do certain things and gain points for it. To simplify this question let's say we got 2 tables user and points.
-- table user -- table points
+---------------+ +-----------------------------+
| id | name | | id | points | user_id |
+---------------+ +-----------------------------+
| 1 Tim | | 1 5 1 |
| 2 Tom | | 2 10 1 |
| 3 Marc | | 3 5 1 |
| 4 Tina | | 4 12 2 |
| 5 Lutz | | 5 2 2 |
+---------------+ | 6 7 1 |
| 7 40 3 |
| 8 100 1 |
+-----------------------------+
Now to get the complete highscore-list I use the following query
SELECT u.*, SUM( p.points ) AS sum_points
FROM user u
LEFT JOIN points p ON p.user_id = u.id
GROUP BY u.id
ORDER BY sum_points DESC
resulting in a fine highscore-list with all users from first to last
+------------------------------+
| id | name | sum_points |
+------------------------------+
| 1 Tim 127 |
| 3 Marc 40 |
| 2 Tom 14 |
| 4 Tina 0 |
| 5 Lutz 0 |
+------------------------------+
Alright back to the question itself. On the profile of a single user I'd like to show his ranking within the highscore-list.
Can this be done using a single query just showing that for example Tom (id=2) is ranked in place 3?
Thanks alot :-)
The idea is to ask, "how many players rank above #this_user":
select count(*) + 1 from
(
/* list of all users */
SELECT SUM( p.points ) AS sum_points
FROM user u
LEFT JOIN points p ON p.user_id = u.id
GROUP BY u.id
) x
/* just count the ones with higher sum_points */
where sum_points > (select sum(points) from points where user_id = #this_user)
Edited to make result 1-based instead of 0-based
SELECT q.*,
#r := #r + 1 AS rank
FROM (
SELECT #r := 0
) vars,
(
SELECT u.*,
SUM(p.points) AS sum_points
FROM
user u
LEFT JOIN
points p
ON p.user_id = u.id
GROUP BY
u.id
ORDER BY
sum_points DESC
) q