合并2个不同的表而不重复PHP值

时间:2017-01-18 13:08:13

标签: php mysql arrays json

我有2张桌子;第一个是user_in_event

这里的角色可以是.slideshow img { margin: 15px 15px 45px; border: 1px solid #ccc; background-color: #eee; }

,第二个是user

此处角色可以是regular, waiting for ack or moderator

我想从user_in_event表中获取角色,基于event_id并输出所有用户信息(在用户角色旁边)并输出一个JSON

我尝试使用regular or admin

LEFT JOIN

但我弄乱了数据

$query = "SELECT user_in_event.role, user_in_event.user_id, user.name, user.email, user.birthday, user.phone_number, user.address, user.image
                    FROM user LEFT JOIN user_in_event 
                    ON user_in_event.event_id = '".$event_id."' 
                    LIMIT $num_of_rows";


        $result = mysqli_query($con,$query)
         or die(mysqli_error($con));

        // print_r($query);

        while ($row = mysqli_fetch_assoc($result)) { 
            $response[] = $row; 
        }




    header('Content-Type:Application/json');
    echo json_encode($response);

PS:我还试图制作2个不同的查询,并将JSON和用户[ { "role": "moderator", "user_id": "2", "name": "ofir", "email": "ofir@ofr.com", "birthday": "08/12/2016", "phone_number": "123", "address": "yoqneam", "image": "http://imgur.com/a/KslOW" }, { "role": "waiting for ack", "user_id": "21", "name": "ofir", "email": "ofir@ofr.com", "birthday": "08/12/2016", "phone_number": "123", "address": "yoqneam", "image": "http://imgur.com/a/KslOW" } ] 结合起来,但我有2个子数组而不是单个数组

1 个答案:

答案 0 :(得分:1)

请尝试:

SELECT user_in_event.role, user_in_event.user_id, user.name, user.email
FROM user
LEFT JOIN user_in_event ON user_in_event.user_id = user.user_id
WHERE user_in_event.event_id =25
LIMIT 0 , 30

这里event_id是静态的。将此查询运行到您的数据库并告诉我。