简体   繁体   English

在 PHP 中使用多个 SQL 语句获取单个结果查询

[英]Using multiple SQL Statements to get single result query in PHP

I am looking to create 2 teams.我正在寻找创建 2 个团队。 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.请参阅SQLfiddle以获得视觉效果。

So I am creating weight after the sorting.所以我在排序后创造了weight 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):现在,我想使用我发现的用于拉奇数行的代码从该weight列中提取我的数据(现在应该拉奇数的加权排序):

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?由于weightAS列,我该如何使用这两个语句? and what would my while($rowid = mysqli_fetch_array($result)) {} look like?我的while($rowid = mysqli_fetch_array($result)) {}会是什么样子?

If you want to select only the odd numbered rows from your SQL query then you should use weight%2=1 instead.如果您只想 select 查询 SQL 查询中的奇数行,那么您应该使用weight%2=1代替。

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.我不知道您为什么使用while($rowid = mysqli_fetch_array($result)) {}但您的 mysqli 结果将包含查询返回的确切数据。 You can use foreach instead though.不过,您可以改用foreach

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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