简体   繁体   中英

select 3 rows from mysql table, then get each row's specific column

  1. Select 3 rows from table1
  2. Get a specific column data out of each row.
  3. Then use that each column data obtained , to make a query again to get data from table2.
  4. Store the data obtained in step 4 into a variable for each row.
  5. Then put them in json array (table 1 , 3 rows + table 2's data(each of them).

I am building a rank table, it displays top 3 users with their rank name. For example: User1 has 2000 points , user 2 has 4000points , user 3 has 10k points , so the top 3 user is : user 3 > user 2 > user 1

So , i want the php to go to 'users' table and get the top 3 members using this:

$query = mysql_query("SELECT * FROM users ORDER BY pts DESC LIMIT 3");
$rows = array();
while($r = mysql_fetch_assoc($query)) {
   $rows[] = $r;
}

Table structure for ' user ':
1.username(varchar)
2.pts(int)

After the rows are put into an array , how can i get 'points' for each of the row in that array. Then go to ' rank ' table to get their ranknames .

Table structure for ' rank ':
1.rank(varchar)
2.pts(int)

Inside rank table there is 'pts' to let php choose compare which rank the user is at based on the points from each row of the array.

Normally i would use this if its only for 1 user , but for multiple users , im not sure:

$result = mysql_query("SELECT * FROM rank WHERE pts <= '$upts' ORDER BY pts DESC LIMIT 1")
              or die(mysql_error()); 

Then after getting the rank for the top 3 users , php will now add the ranks to each of the user(row) in that array(of course , add it to the rank owner, not just simply place it in).

Then JSON encode it out.

How can i do this?

I am not sure if this is what you want. That is combine the two query into one query. Please take a look at http://sqlfiddle.com/#!2/ad419/8

SELECT user.username,user.pts,rank.rank 
FROM user LEFT JOIN rank 
ON user.pts <=rank.pts group by user.id

UPDATED:

For extracting top 3, could do as below;

SELECT user.username,user.pts,rank.rank 
FROM user LEFT JOIN rank 
ON user.pts <=rank.pts 
GROUP BY user.id 
ORDER BY pts DESC LIMIT 3

If i understand correctly, you need to get values from Rank and Users tables. In order to do that in just one query You need to add FK (Foreign Key) to the Rank table that points to a specific user in the Users table.

So you need to add userId to the Rank table and then you can run:

SELECT r.rank, u.points from users u,rank r where u.userId = r.userId

This is roughly what you need.

Not quite the answer to your exact question, but this might be of use to you: How to get rank using mysql query . And may even mean that you don't require a rank table. If this doesn't help, I'll check back later.

Use this query

$query = "SELECT
u.pts,
r.rank
FROM users as u
left join ranks as r
on r.pts = u .pts
ORDER BY pts DESC
LIMIT 3";

This will bring what you required without putting into an array

   $rec = mysql_query($query);
   $results = arrau(); 
   while($row = mysql_fetch_row($rec)){
        $results[] = $row;
   }

   echo json_encode($results);

It looks like what you're trying to do is retrieve the rank with the highest point requirement that the user actual meets, which isn't quite what everyone else is giving here. Fortunately it is easily possible to do this in a single query with a nice little trick:

SELECT 
user.username,
SUBSTRING_INDEX(GROUP_CONCAT(rank.rank ORDER BY pts DESC),",",1) AS `rank`
FROM user 
LEFT JOIN rank ON user.pts >= rank.pts 
GROUP BY user.id 
ORDER BY pts DESC 
LIMIT 3

Basically what the second bit is doing is generating a list of all the ranks the user has achieved, ordering them by descending order of points and then selecting the first one.

If any of your rank names have commas in then there's another little tweak we need to add on, but I wouldn't have thought they would so I've left it out to keep things simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM