简体   繁体   English

选择不同的选定行

[英]Select distinct of selected rows

I'm selecting rows from two tables 我正在从两个表中选择行

$a = mysql_query("SELECT a.id, a.id_user, a.id_location, a.id_event, a.date FROM event as a LEFT JOIN rating as b ON a.id_event = b.id_event AND a.id_user = b.id_whos_get_rate WHERE a.id_user <> $log1[id] AND EXISTS ( SELECT 1 FROM event as c WHERE a.id_event = c.id_event AND c.id_user = $log1[id] ) AND b.id IS NULL ORDER BY DATA") ;

then I'm receiving array 然后我正在接收数组

\n\n

a.id | a.id | a.id_user | a.id_user | a.id_location | a.id_location | a.id_event | a.id_event | a.date 一个约会\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++\n 1 2 1 2 20.03.2013 1 2 1 2 20.03.2013\n 2 3 1 2 20.03.2013 2 3 1 2 20.03.2013\n 3 4 1 2 20.03.2013 3 4 1 2 20.03.2013\n 4 12 1 3 20.03.2013 4 12 1 3 20.03.2013\n 5 13 1 3 20.03.2013 5 13 1 3 20.03.2013\n 6 14 1 3 20.03.2013 6 14 1 3 20.03.2013

Then I want to echo that values and adding radio by each user what I'm doing that way: 然后,我想回显该值,并由每个用户添加无线电,以这种方式进行操作:

 <pre>>while($b = mysql_fetch_array($a)){ >$place = mysql_query("select * from name_event where id=$b[id_location]"); >$place1= mysql_fetch_array($place); >echo $place1[name]; >echo $b[date].; >echo "id event: ".$b[id_event].; >$name= mysql_query("select* from users where id='".$b[id_user]."'and id!=$log1[id]"); >$name1= mysql_fetch_array($name); >if (!empty($name1[login])){ >echo $name1[login]. ; >echo "form method ='post' action ='rate.php' >rate >input type='radio' name='s' value='$name1[id]|1|$b[id_event]' >input type='radio' name='s' value='$name1[id]|2|$b[id_event]' >input type='submit' name='submit' value='value' 

the problem is that a.id_event has multiple values and I have no clue how to change the code to echo only once a.id_event and then all users which taking part in that event because at the moment it's echo 问题是a.id_event具有多个值,我不知道如何将代码更改为仅回显一次a.id_event,然后再回显参与该事件的所有用户,因为此刻回显

id_event 2 
id_user 2
id_user 3
id_user 4

and I'm expecting to work like that 我希望能像那样工作

\nid_event 2 id_event 2 \nid_user 2 id_user 2\nid_user 3 id_user 3\nid_user 4 id_user 4\n

As long as you have your data ordered by the id_event and therefore the values are consecutive, I think the best answer to this would just to have a variable in your loop to contain last's id_event and just print the id_user if it's being repeated. 只要您的数据按id_event ,因此值是连续的,我认为对此的最佳答案就是在循环中包含一个变量以包含last_id_event并在重复时仅打印id_user。

As an example: 举个例子:

$last_id_event = null;
while($row = mysql_fetch_array($a)){ // Each row
    if($row['id_event'] != $last_id_event){
        echo "id event: ".$row['id_event'];
        $last_id_event = $row['id_event']
    }
    echo "id user: ".$row['id_user'];
}

EDIT: changed foreach to while(... mysql_fetch_array), which is the correct way to go. 编辑:foreach更改为while(... mysql_fetch_array),这是正确的方法。

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

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