WHERE and AND in MySQL Query

Asked
Active3 hr before
Viewed126 times

8 Answers

query
90%

WHERE Clause in MySQL is a keyword used to specify the exact criteria of data or rows that will be affected by the specified SQL statement. The WHERE clause can be used with SQL statements like INSERT, UPDATE, SELECT, and DELETE to filter records and perform various operations on the data.,The WHERE condition in MySQL when used together with the AND logical operator, is only executed if ALL filter criteria specified are met., The SQL WHERE clause is used to restrict the number of rows affected by a SELECT, UPDATE or DELETE query.,The basic syntax for the WHERE clause when used in a MySQL SELECT WHERE statement is as follows.

The basic syntax for the WHERE clause when used in a MySQL SELECT WHERE statement is as follows.

SELECT * FROM tableName WHERE condition;
SELECT * FROM `members`
WHERE `membership_number` = 1;

Executing the above script in MySQL workbench on the “myflixdb” would produce the following results.

membership_number

full_names

gender

date_of_birth

physical_address

postal_address

contct_number

email

1

Janet Jones

Female

21 - 07 - 1980

First Street Plot No 4

Private Bag

0759 253 542

janetjones @yagoo.cm
SELECT * FROM `movies`
WHERE `category_id` = 2 AND `year_released` = 2008;
movie_id

title

director

year_released

category_id

2

Forgetting Sarah Marshal

Nicholas Stoller

2008

2
SELECT * FROM `movies`
WHERE `category_id` = 1 OR `category_id` = 2;
movie_id

title

director

year_released

category_id

1

Pirates of the Caribean 4

Rob Marshall

2011

1

2

Forgetting Sarah Marshal

Nicholas Stoller

2008

2
SELECT * FROM `members`
WHERE `membership_number`
IN(1, 2, 3);

Executing the above script in MySQL workbench against the “myflixdb” produces the following results.

membership_number

full_names

gender

date_of_birth

physical_address

postal_address

contct_number

email

1

Janet Jones

Female

21 - 07 - 1980

First Street Plot No 4

Private Bag

0759 253 542

janetjones @yagoo.cm

2

Janet Smith Jones

Female

23 - 06 - 1980

Melrose 123

NULL

NULL

jj @fstreet.com

3

Robert Phil

Male

12 - 07 - 1989

3 rd Street 34

NULL

12345

rm @tstreet.com
SELECT * FROM `members`
WHERE `membership_number`
NOT IN(1, 2, 3);
membership_number

full_names

gender

date_of_birth

physical_address

postal_address

contct_number

email

4

Gloria Williams

Female

14 - 02 - 1984

2n d Street 23

NULL

NULL

NULL
SELECT * FROM `members`
WHERE `gender` = 'Female';
membership_number

full_names

gender

date_of_birth

physical_address

postal_address

contct_number

email

1

Janet Jones

Female

21 - 07 - 1980

First Street Plot No 4

Private Bag

0759 253 542

janetjones @yagoo.cm

2

Janet Smith Jones

Female

23 - 06 - 1980

Melrose 123

NULL

NULL

jj @fstreet.com

4

Gloria Williams

Female

14 - 02 - 1984

2n d Street 23

NULL

NULL

NULL
payment_id

membership_number

payment_date

description

amount_paid

external_reference_number

1

1

23 - 07 - 2012

Movie rental payment

2500

11

3

3

30 - 07 - 2012

Movie rental payment

6000

NULL
SELECT * FROM `movies`
WHERE `category_id` < > 1;
movie_id

title

director

year_released

category_id

2

Forgetting Sarah Marshal

Nicholas Stoller

2008

2

5

Daddy 's Little Girls

NULL

2007

8

6

Angels and Demons

NULL

2007

6

7

Davinci Code

NULL

2007

6

9

Honey mooners

John Schultz

2005

8
SELECT * FROM `movierentals`
WHERE `return_date` < '2012-06-25'
AND movie_returned = 0;
reference_number

transaction_date

return_date

membership_number

movie_id

movie_returned

14

21 - 06 - 2012

24 - 06 - 2012

2

2

0
load more v
88%

The MySQL AND condition and OR condition can be combined in a SELECT, INSERT, UPDATE, or DELETE statement.,This next AND & OR example demonstrates how the AND condition and OR condition can be combined in the INSERT statement.,Let's look at an example that combines the AND and OR conditions in a SELECT statement.,The syntax for the AND condition and OR condition together in MySQL is:

The syntax for the AND condition and OR condition together in MySQL is:

WHERE condition1
AND condition2
   ...
   OR condition_n;
load more v
72%

MySQL evaluates the WHERE clause after the FROM clause and before the SELECT and ORDER BY clauses.,Use the WHERE clause to filter rows by a condition.,When executing a SELECT statement with a WHERE clause, MySQL evaluates the WHERE clause after the FROM clause and before the SELECT and ORDER BY clauses:,Summary: in this tutorial, you will learn how to use the MySQL WHERE clause in the SELECT statement to filter rows from the result set.

The WHERE clause allows you to specify a search condition for the rows returned by a query. The following shows the syntax of the WHERE clause:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
SELECT
select_list
FROM
table_name
WHERE
search_condition;
Code language: SQL(Structured Query Language)(sql)
load more v
65%

I don't understand. You got 3 movies and 3 genres, where are this four numbers coming from? This query returns exactly 3, 8, 5 - i have tested it before posting as answer. – Nikola Markovinović Mar 21 '12 at 8:55 , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Hmm the first results were ok, but the following not. I've posted another example on pastebin Please keep in mind, I'm using 3 Genres just as an example. Its possible that the query can have 5 Genres in the WHERE clause – richie Mar 21 '12 at 9:43 , I think you understand me :) Because the second query youve posted is doing exactl what i want! :) Thank you soo much – richie Mar 21 '12 at 9:02

If I understand correctly you want to sort results by number of matches in descending order. To do so, you might try:

SELECT movie
FROM genre_rel
WHERE genre IN(1, 8, 3)
GROUP BY movie
order by count(movie) desc

And if you want movies that match all the criteria, you might use:

SELECT movie
FROM genre_rel
WHERE genre IN(1, 8, 3)
GROUP BY movie
HAVING count(movie) = 3

This is the best I can do in MySql. You cannot use IN because you cannot extract information about order of filters. If you add derived table as a means of filtering, you can append this information and use it to show results by positional matches. Note that you do not provide any ordering info in genre_rel table so you don't really know the importance of genres per movie. This query will give you matching movies by descending order of importance of genres in criteria:

SELECT movie
FROM genre_rel
INNER join
   (
      select 1 genre, 1000 weight union all select 8, 100 union all select 3, 10
   ) weights
on genre_rel.genre = weights.genre
GROUP BY movie
order by sum(weight) desc
load more v
75%

The following code block has a generic SQL syntax of the SELECT command with the WHERE clause to fetch data from the MySQL table −,This will use the SQL SELECT command with the WHERE clause to fetch the selected data from the MySQL table – tutorials_tbl.,We have seen the SQL SELECT command to fetch data from a MySQL table. We can use a conditional clause called the WHERE Clause to filter out the results. Using this WHERE clause, we can specify a selection criteria to select the required records from a table.,You can specify any condition using the WHERE clause.

The following code block has a generic SQL syntax of the SELECT command with the WHERE clause to fetch data from the MySQL table −

SELECT field1, field2, ...fieldN table_name1, table_name2...[WHERE condition1[AND[OR]] condition2.....
load more v
40%

The WHERE clause is used to filter records.,The WHERE clause is used to extract only those records that fulfill a specified condition.,The following example selects the id, firstname and lastname columns from the MyGuests table where the lastname is "Doe", and displays it on the page:,First, we set up the SQL query that selects the id, firstname and lastname columns from the MyGuests table where the lastname is "Doe". The next line of code runs the query and puts the resulting data into a variable called $result.

First, we set up the SQL query that selects the id, firstname and lastname columns from the MyGuests table where the lastname is "Doe". The next line of code runs the query and puts the resulting data into a variable called $result.

Then, the function num_rows() checks if there are more than zero rows returned.

function num_rows()
load more v
22%

MySQL IN() function finds a match in the given arguments.,Example: MySQL IN() function ,▼MySQL Comparison functions and operator,MySQL Functions and Operators

Syntax:

expr IN(value, ...)
load more v
60%

SELECT is used to retrieve rows selected from one or more tables, and can include UNION statements and subqueries. See Section 13.2.10.3, “UNION Clause”, and Section 13.2.11, “Subqueries”. A SELECT statement can start with a WITH clause to define common table expressions accessible within the SELECT. See Section 13.2.15, “WITH (Common Table Expressions)”. , table_references indicates the table or tables from which to retrieve rows. Its syntax is described in Section 13.2.10.2, “JOIN Clause”. , SELECT can also be used to retrieve rows computed without reference to any table. , SQL_BUFFER_RESULT forces the result to be put into a temporary table. This helps MySQL free the table locks early and helps in cases where it takes a long time to send the result set to the client. This modifier can be used only for top-level SELECT statements, not for subqueries or following UNION.

A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables:

SELECT * FROM t1 INNER JOIN t2...
load more v

Other "query-undefined" queries related to "WHERE and AND in MySQL Query"