Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT WHERE IN - mySQL

let's say I have the following Table:

 ID, Name 
 1, John 
 2, Jim 
 3, Steve 
 4, Tom

I run the following query

SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');

I want to get something like:

ID
1
2
NULL or 0

Is it possible?

like image 883
vshale Avatar asked Aug 04 '13 03:08

vshale


People also ask

What is WHERE clause in MySQL?

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.

Can we use WHERE in select?

The basic syntax for the WHERE clause when used in a MySQL SELECT WHERE statement is as follows. “WHERE” is the keyword that restricts our select query result set and “condition” is the filter to be applied on the results. The filter could be a range, single value or sub query.

What is select from WHERE in SQL?

The SQL SELECT statement returns a result set of records, from one or more tables. A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used data manipulation language (DML) command.

How do I select data in MySQL?

Introduction to MySQL SELECT statement First, specify one or more columns from which you want to select data after the SELECT keyword. If the select_list has multiple columns, you need to separate them by a comma ( , ). Second, specify the name of the table from which you want to select data after the FROM keyword.


1 Answers

How about this?

SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill')
   UNION
SELECT null;
like image 175
ChaiNavawongse Avatar answered Sep 18 '22 16:09

ChaiNavawongse