重复数据库中的数据?

重复数据库中的数据?

问题描述:

我想告诉你我有什么问题需要帮助我。

我在数据库中创建了一个包含以下列的表格:



表:union

Hi, I would like to tell you about the problem I have to see if someone can help me.
I have created a table in a database with the following columns:

Table: union

exercise_id_fk	tag_id_fk
   1	             1
   1	             2
   2	             1
   2	             3

我假装我的程序是用户选择他们想要的标签,以便在屏幕上显示与所选标签相对应的练习。例如,如果用户选择标签1,则会出现练习1和2。如果选择标签1和3,则会出现练习1和2.



我的程序的问题是,当用户选择标签1和3时,例如,练习1和2出现但重复2次。或者当您选择标签1和2时,重复练习1.



有人知道任何方式以便不会发生这种情况吗?只展示一次练习?



我尝试过:



这里我展示了我的部分代码:

What I pretend with my program is that a user chooses the tags they want so that the exercise corresponding to the chosen tags is shown on the screen. For example, if the user chooses tag 1, exercises 1 and 2 will appear. If you choose tags 1 and 3, exercises 1 and 2 appear.

The problem with my program is that when the user chooses tags 1 and 3, for example, exercises 1 and 2 appear but 2 repeats. Or when you choosing tags 1 and 2, exercise 1 is repeated.

Does anybody know any way so that this does not happen? Only show the exercises once?

What I have tried:

Here I show part of my code:

$sql = "SELECT * FROM exercises, union, tags where exercise_id = exercise_id_fk and tag_id = tag_id_fk";

if (!empty($_SESSION['tags_array'])) {
    $sql .= " and (";
    foreach ($_SESSION['tags_array'] as $tagId)
        $sql .= 'tag_id = ' . $tagId . ' or ';

    $sql .= "tag_id = -1);";
}

$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
    echo $row["exercise_id"] . ". " .  $row["title"] . "<br>";
}

sql = SELECT * FROM exercise,union,tags其中exercise_id = exercise_id_fk和tag_id = tag_id_fk;

if (!empty(
sql = "SELECT * FROM exercises, union, tags where exercise_id = exercise_id_fk and tag_id = tag_id_fk"; if (!empty(


_SESSION [ ' tags_array'])){
_SESSION['tags_array'])) {


sql 。= 和(;
foreach