簡體   English   中英

如何獲取除特定值之外的所有行

[英]how to get all rows except with specific values

您好,有沒有辦法只獲取組織尚未使用的survey_name

`survey table`
+-----------+-------------+
| survey_id | survey_name |
+-----------+-------------+
|         1 | name1       |
|         2 | name2       |
|         3 | name3       |
|         4 | name4       |
|         5 | name5       |
+-----------+-------------+

`link table`
+---------+-------------+-----------------+
| link_id | survey_link | organisation_id |
+---------+-------------+-----------------+
|       1 | 1(survey_id)|        1        |
|       2 | 2           |        1        |
|       3 | 2           |        2        |
|       3 | 3           |        2        |
|       3 | 6           |        2        |
+---------+-------------+-----------------+

在此數據庫結構中,您會看到每個組織的信息:

可用的調查組織1:

  • 姓名3
  • 姓名 4
  • 姓名5

可用的調查組織1:

  • 姓名1
  • 姓名 4
  • 姓名5

我試過使用WHERE NOT (survey_id= $row['survey_id'])

//get all survey's that are in use
$sqlCheckLink = "SELECT `survey_id` FROM `link_info` WHERE `organisation_id`=".$_SESSION['organisation_id']."";
    $resultCheck = mysqli_query($conn, $sqlCheckLink);
    if ($resultCheck ->num_rows > 0) {
        while ($id = $resultCheck -> fetch_assoc()) {
            //show all surveys that are available
            $sqlGetSurveys = "SELECT * FROM `survey_info` WHERE NOT (survey_id = ".$id['survey_id'].")";
            $resultAllSurveys = mysqli_query($conn, $sqlGetSurveys);

            if ($resultAllSurveys ->num_rows > 0) {
                while ($row = $resultAllSurveys-> fetch_assoc()) {
                   //echo content
                }
            }
        }
    }

這里但它似乎不起作用......使用這種方法我也得到了正在使用的調查。

如果有人可以提供幫助,將不勝感激!

只需使用左連接:

SELECT t.survey_name
FROM survey_table as t
LEFT JOIN link_table AS l ON l.survey_link = t.survey_id
WHERE l.link_id IS NULL

未經測試,但您可以了解這個想法。 我希望這對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM