MS SQL php查询逗号分隔列?

时间:2015-09-03 15:59:43

标签: php mysql odbc

我对MS SQL查询完全不熟悉,我所拥有的是三个表,如下所示:

将列(userGroupLocID)设置为varchar数据类型的Users表,此字段包含以逗号分隔的值,例如(1,2)

具有列(user_loc_id)的Room_Location表设置了数据类型INT

Client_Room表,其列(id)设置为数据类型INT

我有以下查询,$query = "SELECT * FROM client_room, room_location, users WHERE $useractiveid = (users.id) AND client_room.id = (room_location.user_loc_id)";

这会从Room_Location表输出一个完整的位置列表,这是正常的,但我只希望在用户表中存在id的位置显示(userGroupLocID)$query = "SELECT * FROM client_room, room_location, users WHERE $useractiveid = (users.id) AND client_room.id = (room_location.user_loc_id) AND cast(client_room.id as varchar(10)) IN (users.userGroupLocID)";

有人可以告诉我,我的错误在哪里。

1 个答案:

答案 0 :(得分:0)

我最终设法解决了我自己的问题,它可能不是最好的答案,但这是它在MS SQl上工作的唯一方式。

<select name="event_room" id="event_room">
                        <option>Room Location</option>
                        <?php 

                            $query = "SELECT * FROM users WHERE theusername = '$userident'";

                            $result = odbc_exec($connect, $query);

                            while($row = odbc_fetch_array($result)){

                                $usegrids = explode(',', $userperenco);
                                foreach ($usegrids as $usegrid) {
                                    $querygroup = "SELECT * FROM room_location WHERE user_loc_id = '$usegrid'";
                                    $resultgroup = odbc_exec($connect, $querygroup);
                                    while($rowgroup = odbc_fetch_array($resultgroup)){
                                        echo "<option value=\"" . $rowgroup['location'] . "\">" . $rowgroup['location'] . "</option>\n";
                                    }
                                }
                            }
                        ?>
                    </select>