table name = users, id is primary key and of type int, name is of type text
id
name
1
user1
2
user2
table name = courses, id is primary key and of type int, name is of type text and userid is a foreign key for the ids in table users and is of type int
id < /th>
name < /th>
userId < /th>
< /tr>
< /thead>
1 < /td>
course1 < /td>
1 < /td>
cours />
2
course2
1
3
course3
2
4
course4
2
table name = modules, id is primary key and of type int, name is of type text, userid is the foreign key for the ids in the users table, and courseid is another foreign key for the ids in the courses table
id
name
userid
courseId < /th>
< /tr>
< /thead>
1 < /td>
module1 < /td>
1 < /td>
1 < /td>
< /td>
< /td>
< /td>
1 < /td> /> 2 < /td>
module2 < /td>
1 < /td>
3 < /td>
< /tr>
3 < /td>
module3 < /td>
/>3
4
module4
1
4
Я пытаюсь построить запрос SQL, который может вернуть строки в таблице модулей, где пользователь и курс содержат значения, которые обычно не должны быть результатом первичных идентификаторов /иностранных ключей, показанных в таблице курсов (я пытаюсь решить потенциальную повреждение данных). Строки, где внешний ключ (пользователь) составляет 1, а основной идентификатор (курс)-3, и нет строк, где внешний ключ (пользователь)-1, а основной идентификатор (курс)-4. Таким образом, выход: < /p>
name
userid
courseid
2
module2
1
3
4
module4
1
4
Я не уверен, как использовать Union /
Код: Выделить всё
intersectionПодробнее здесь: https://stackoverflow.com/questions/794 ... alues-meet
Мобильная версия