MySQL query method with multiple conditions

MySQL query method with multiple conditions

mysql query with multiple conditions

Environment: MySQL 5.7

When multiple and or ors appear in a where statement, you need to enclose multiple ORs in parentheses and then combine them with AND, or enclose multiple ANDs in parentheses and then combine them with OR.

mysql many-to-many conditional query

Two tables user and role, the middle table is user_role

Query the correspondence between users and roles

select res.user_name,r.role_name from(select u.user_name,ur.role_id from user as u INNER JOIN user_role as ur where u.user_id=ur.user_id) as res 
INNER JOIN role as r where res.role_id=r.role_id;

mysql query to find multiple conditions in the same field

analyze:

1. First, find out the courses that student No. 01 is studying

SELECT C FROM sc WHERE S='01' 

2. Query the student numbers of the relevant courses

SELECT S FROM sc WHERE C in 
(SELECT C FROM sc WHERE S='01') 

As you can see, we queried three times, so there are multiple results. Because it is an or relationship, all students who selected courses 1, 2, and 3 are retrieved.

3. Now we need to take out the number that appears 3 times

SELECT S FROM sc WHERE C in 
(SELECT C FROM sc WHERE S='01') 
GROUP BY S HAVING COUNT(S) = 3 

I saw that students numbered 1, 2, 3, and 4 chose the same course as student numbered 01.

4. Perform a join query with the student table to retrieve relevant information

SELECT a.* FROM
student a LEFT JOIN sc b
ON aS = bS WHERE bS in (
SELECT S FROM sc WHERE C in 
(SELECT C FROM sc WHERE S='01') 
GROUP BY S HAVING COUNT(S) =3)
GROUP BY aS 

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • The difference and reasons between the MySQL query conditions not in and in
  • MySQL complete collapse: detailed explanation of query filter conditions
  • MySQL conditional query and or usage and priority example analysis
  • Detailed explanation of the problem of matching even when there is a space at the end of the string in the Mysql query condition
  • Detailed explanation of common usage of MySQL query conditions
  • Will the index be used in the MySQL query condition?

<<:  Common properties of frameset (dividing frames and windows)

>>:  Problems and experiences encountered in web development

Recommend

Complete steps for Nginx to configure anti-hotlinking

need: Usually, sites want to prevent videos and p...

Explanation of the problem that JavaScript strict mode does not support octal

Regarding the issue that JavaScript strict mode d...

Example of how to set up a third-level domain name in nginx

Problem Description By configuring nginx, you can...

Detailed explanation of vue.js dynamic components

:is dynamic component Use v-bind:is="compone...

Five guidelines to help you write maintainable CSS code

1. Add a comment block at the beginning of the sty...

Detailed explanation of MySQL 8.0 atomic DDL syntax

Table of contents 01 Introduction to Atomic DDL 0...

Simple steps to write custom instructions in Vue3.0

Preface Vue provides a wealth of built-in directi...

Vue3 based on script setup syntax $refs usage

Table of contents 1. Vue2 syntax 2. Use of Vue3 1...

MySQL knowledge points for the second-level computer exam mysql alter command

Usage of alter command in mysql to edit table str...

Solution to the problem of failure to insert emoji expressions into MySQL

Preface I always thought that UTF-8 was a univers...

Search optimization knowledge to pay attention to in web design

1. Link layout of the new site homepage 1. The loc...

A brief discussion on the fun of :focus-within in CSS

I believe some people have seen this picture of c...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

JavaScript code to implement a simple calculator

This article example shares the specific code of ...