I created a webstore and I put a top donator per month.
My problem is, how I can get "top donator"?
I try in SQL query MAX(money) but have problem on example:
test 250
test2 3
And say max value is 3
My table:
Date | username | money
2016/7 | tg95 | 10
2016/7 | test | 15
How I can get the top donator perm amount?
Here is a table config example:
date | username | money
2016/7 | test | 10
2016/7 | test2 | 15
2016/7 | test3 | 9
on this example max value is test2 = 15. Maybe more simple is get date (per month) and get a max value on money.
Try:
SELECT username FROM Table1 ORDER BY money DESC LIMIT 1;
SQL FIDDLE
If you want to select username of max donator money you can use this sql
select username, money
from youtablename
where money= (
select max(money)
from youtablename
)
Related
Database data:
id | account | date | random_data
1 | 1 | 01/01/2013 | qw
2 | 2 | 05/01/2013 | er
3 | 2 | 09/01/2013 | ty
4 | 1 | 05/01/2013 | ui
5 | 2 | 11/01/2013 | op
6 | 1 | 12/01/2013 | as
Hi, so let's say I want the records starting from 05/01/2013 - note that prev_date for the 1st row still shows an earlier date than 05/01 meaning that the whole table still needs to be searched.
Result data:
account | cur_date | random_data | prev_date | prev_rand_data
1 | 05/01/2013 | ui | 01/01/2013 | qw
1 | 12/01/2013 | as | 05/01/2013 | ui
2 | 05/01/2013 | er | null | null
2 | 09/01/2013 | ty | 05/01/2013 | er
2 | 11/01/2013 | op | 09/01/2013 | ty
So I'm not sure what is the best, most optimized query I could use for this. I'm not opposed to a php solution but not sure how much better that would be. Some ideas I've considered:
Some sort of join on the same table - not sure how though
Sub queries on the select -
select date as cur_date
, (select max(date)
from table
where date < cur_date
group by account)
as prev_date... - this seems like it could be incredibly intensive
Session variables - set a session variable on each row which will be the previous data for the next row e.g.
select date as cur_date
, #prev_date as prev_date
, #prev_date:=date...
Has anyone had any experience with a problem like this and was there a good solution? Are there any positives negatives with any of the ideas I have that could cause problems in the future?
I would use a combination of sql and application code. Since I am not a php programmer, I will only describe the logic to use for the application part.
First the query.
select account, date, random_data
from thetable
where date >= YourDateVariable
union
select account, date, random_data
from thetable join
(select account acc, max(date) maxdate
from thetable
where date <= YourDateVariable
group by account) x on account = acc and date = max(date)
where date <= YourDateVariable
order by account, date
For the application code, do this:
Set a variable called ThisAccount to 0.
Set a row counter variable to 0.
Create an empty 2D array
Start looping through your query results
Put the account value and random data into the first two columns
of the next available row of the array
Compare the account value to the value of the ThisAccount variable.
If they are the same, get the previous date and random data from
the previous row in the array.
Set the ThisAccount variable to the current account value.
Increment your row counter variable
End of loop.
I'm trying to wrap my head around how to get a distinct count of days a user logged in, when the db has each login session stored with a time and date stamp (DATETIME column). EG:
USERID | TIME | BOUGHT
--------------------------
4 | 2012-07-16 04:44:52 | 3
4 | 2012-07-16 04:45:52 | 2
5 | 2012-07-16 04:54:52 | 5
4 | 2012-07-18 04:44:52 | 3
4 | 2012-07-18 04:45:52 | 2
4 | 2012-07-21 04:54:52 | 5
I want to search for how many times user 4 bought from the site - and the query should return 3 (because user 4 visited the site a total of 3 days). Do I have to use a PHP/MYSQL combination or can I do it in SQL?
SELECT USERID, COUNT(DISTINCT DATE(TIME)) FROM my_table GROUP BY USERID
See it on sqlfiddle.
I have a table where the user will select a category and enter a search parameter, and I have to find the latest value in hat category.
An example of a dataset might be:
ID | category | name | price | date
1 | bubblegum | Steve | 2.00 | 2011/1/1
2 | bubblegum | Bob | 1.00 | 2011/1/8
3 | bubblegum | Steve | 1.80 | 2011/2/1
If the user searched for "Steve", they would get only the latest price that also has the name "steve" in it.
Max(date) doesn't seem to work though, and I don't understand why. I think it is because if, were I to look for "bob", because Bob doesn't appear in the Max(date), it won't show up. But maybe I am using the SELECT parameter wrong.
SELECT *
FROM yourtable
ORDER BY date DESC
WHERE name='Steve'
LIMIT 1
SELECT * FROM yourtable WHERE name='Steve' ORDER BY date DESC LIMIT 1
With PHP and MySQL I am trying to display items with most votes over a certain period of time (24 hour, 1 week, 1 year, etc). When someone votes on an item, a table records the user, item id, vote, and time, like so:
Table1
username | itemid | vote | time
asdf | 127 | 1 | 1306726126
asdf | 124 | -1 | 1306726123
bob | 127 | 1 | 1306726129
bob | 124 | 1 | 1306726123
Now, I have another table with item details.
Table2
itemid | name | category | date | etc
What I WANT to do is call a table to display all the data from table 2 for only items with votes in the last 24hours and sort it by the votes. This means I need to SUM votes with TIME < 24 hours, then RIGHT JOIN (?) to my other database? I don't know, I am having difficulty figuring out how I should go about doing this. Any suggestiongs?
Something like this should work.
SELECT SUM(Table1.vote) as votes, Table2.* FROM Table2
LEFT JOIN Table1 ON Table1.itemid=Table2.itemid
WHERE Table1.`time`>=DATE_SUB(Table1.`time`, INTERVAL 24 HOUR)
GROUP BY Table1.itemid
ORDER BY Table1.votes DESC
I have a data table having fields(date,company,data_id,rank etc)..as problem is related to these fields that's why showing these fields only. suppose table is:
data_id | company | date | rank
1 | google | 23/10/2010| 1
2 | yahoo | 23/10/2010| 4
3 | msn | 23/10/2010| 8
4 | google | 27/10/2010| 3
5 | yahoo | 27/10/2010| 1
6 | msn | 27/10/2010| 6
7 | google | 29/10/2010| 1
8 | yahoo | 29/10/2010| 4
9 | msn | 29/10/2010| 3
...and so on
PROBLEM 1:
there are many users-suppose there are user1,user2,user3. All have their [my_company] in session.
Now, I have to display only those entries which are made last(can be done by any user on any date) as per company.
Example: my_company[user1-yahoo,user2-google,user3-msn]
user's [my_company] only display his company's value,nothing else..but only value entered last(on date-here 29/10/2010).
Data is added for any company by any user on any date.now as this process will continue, entries will grow.HOW CAN I FIND WHICH DATE IS LAST(specific to a company)?
PROBLEM 2:
how to find closest date to a specific date?
... where `company_name` = 'companyName' order by `date` desc limit 1
and
... between mydate - INTERVAL and mydate + INTERVAL
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
when you create session variable sort data by date so you will have order in my_company
once you have ordered list you can figure out which date belongs to which date
or
while adding data to my_company add id
you can find closest date by
SELECT date FROM table ORDER BY abs(now() - date) LIMIT 1
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_abs