简体   繁体   中英

Using multiple SQL Statements to get single result query in PHP

I am looking to create 2 teams. After research, I have found that I can sort my table, then assign a "ranking" system in the new sorted order. Now, I am trying to pull just the odd number rows (and even number after I figure out odd).

This statement will do the sorting and ranking:

SELECT t.*, @rownum := @rownum + 1 AS weight FROM golfers t, (SELECT @rownum := 0) r WHERE trip_name_table_ID = 1 ORDER BY golfer_handicap

See SQLfiddle for visual.

So I am creating weight after the sorting. Now, I want to pull my data off of that weight column with this code I found for pulling Odd row (which should now be pulling odd number of weighted sort):

SELECT * FROM golfers WHERE weight IN(SELECT weight FROM golfers WHERE weight%2 <> 0);

How do I use both statements off of each other since weight is an AS column? and what would my while($rowid = mysqli_fetch_array($result)) {} look like?

If you want to select only the odd numbered rows from your SQL query then you should use weight%2=1 instead.

The full query should look like this:

SELECT * FROM (
    SELECT t.*, @rownum := @rownum + 1 AS weight 
    FROM golfers t, (SELECT @rownum := 0) r 
    WHERE trip_name_table_ID = 1 
    ORDER BY golfer_handicap
) as t1 
WHERE weight%2=0

I don't know why you are using while($rowid = mysqli_fetch_array($result)) {} but your mysqli result will contain the exact data that your query returned. You can use foreach instead though.

foreach($result as $row) {
    echo $row['golfer_name'];
    echo $row['weight'];
}

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