selecting with a column being one of two possible values - sql

I have a table called "people" with a column named "name". I would like to select all rows where the name is "bob" or "john". I have tried the following and many variants of it, none of which work. How can I do this correctly?
select * from people where name is bob or john;
Thanks

To compare a column with a value you need to use = not IS
select *
from people
where name = 'bob'
or name = 'john';
Alternatively you can use the IN operator.
select *
from people
where name IN ('bob','john');
Note that string comparison is case-sensitive in SQL. So the above will not return rows where the name is Bob or John

Related

SQL - Find specific value in a specific table

Say I have a table called "Team" with the following columns:
ID, MemberName ,ManagerName,Title
And I would like to retrieve all rows where a value "John" exists.
Assume "John" exists in a row for the MemberName column, and that "John" would exist in another row under the "ManagerName" column.
Please assume would have large number of columns. Greater than 50, and do would not know where the value would fall under statically.
If you need an exact match for "John" you can use following query:
Select *
From Team
Where MemberName = 'John' or ManagerName = 'John'
If you need all rows where "John" could be a part of the string then you can use like:
Select *
From Team
Where MemberName like '%John%' or ManagerName like '%John%'
Generally, you need to specify all the columns your are searching from in SQL.
SELECT * FROM Team WHERE 'John' IN (col1, col2, col3, ..., colN) ;
However that depends.
If you are using MySQL you can Search Table Data. From the MySQL Workbench right click the table , and choose Search Table Data.
If you are using PostgreSQL take a look at the following:
https://stackoverflow.com/a/52715388/10436747

inserting multiples values in one column

I have a question about SQL. I have created a table in SQL with only one column containing the name of two people (say John and Matt). Later I added a new column into the table with ALTER TABLE. This column will contain the surname of these people.
My question is, in case mmy table contained several people already is there a command to enter the surnames for all the people at once rather than writing one command for each person as in:
INSERT INTO table (Surname) VALUE (John's surname) and
INSERT INTO table (Surname) VALUE (Matt's surname) ?
Thanks in advance
P.D.
I tried something like:
UPDATE foo set Surname=("Parker","Walker") where Name =("John","Matt") but does not work
You want an update. Something like this:
update t
set surname = 'John'' surname'
where firstname = 'John';
You can do this separately for each name. Or use a case expression for multiple ones:
UPDATE foo
SET Surname = (CASE WHEN Name = 'John' THEN 'Parker'
WHEN Name = 'Matt' THEN 'Walker'
END)
WHERE Name IN ('John', 'Matt');

SQL SERVER Query Select by FirstName, Lastname

My Products table has almost 10,000 records where i want the "artist" ( varchar ) column name to sort by Firstname and Lastname. In the where clause i am sending initial letter of either first name or lastname. This Query works for few records but not for all, its probably because of the pattern of the records ?
Here is my query
SELECT * FROM Products WHERE SUBSTRING(artist, 1, 1) Like 'C'
Doesnt return the records... the way i want it ...Few of the records of the table ( artist) column are below
Comtesse Mathilde duMonceau de Bergendael
Mulholland, S.J.
Cleminson
NULL
Samuel Jackson
Your LIKE clause is wrong. Also you should not be using LIKE at all here, as you're only filtering by one letter:
.... WHERE LEFT(artist, 1) = 'C'
EDIT LEFT is actually faster than SUBSTRING.
Also, you can not expect anything that's not starting with a C to be returned, as you're explicitly only asking for entries that start with a C.
I guess what you really need is the following:
.... WHERE LEFT(FirstName, 1) = 'C' OR LEFT(LastName, 1) = 'C' ORDER BY LastName, Firstname
If you only have the Artist column, things get complicated, as you actually do need to use LIKE, which makes things pretty slow
.... WHERE Artist LIKE `%C%`
returns any artist, the name of which contains a C anywhere in the name. This would also return these:
Clemens Test
Test Cutler
Arachnophobia
User this where clause
where left(artist, 1) = 'C'
or
where artist like 'C%'

MySQL SELECT query string matching

Normally, when querying a database with SELECT, its common to want to find the records that match a given search string.
For example:
SELECT * FROM customers WHERE name LIKE '%Bob Smith%';
That query should give me all records where 'Bob Smith' appears anywhere in the name field.
What I'd like to do is the opposite.
Instead of finding all the records that have 'Bob Smith' in the name field, I want to find all the records where the name field is in 'Robert Bob Smith III, PhD.', a string argument to the query.
Just turn the LIKE around
SELECT * FROM customers
WHERE 'Robert Bob Smith III, PhD.' LIKE CONCAT('%',name,'%')
You can use regular expressions like this:
SELECT * FROM pet WHERE name REGEXP 'Bob|Smith';
Incorrect:
SELECT * FROM customers WHERE name LIKE '%Bob Smith%';
Instead:
select count(*)
from rearp.customers c
where c.name LIKE '%Bob smith.8%';
select count will just query (totals)
C will link the db.table to the names row you need this to index
LIKE should be obvs
8 will call all references in DB 8 or less (not really needed but i like neatness)

MySQL IN with LIKE

How would I use a IN table with like? So that I could use % in them? By in I mean:
SELECT fields
FROM table
WHERE age = "50"
AND name IN ("tim", "bob", "nancy", "john");
I already tried:
SELECT fields
FROM table
WHERE age = "50"
AND name LIKE ("2010-09-17%", "2010-09-16%")
But it gave the error "Operand should contain 1 column(s)"
You can use a number of LIKE expressions:
SELECT fields
FROM table
WHERE age = "50"
AND (
name LIKE "2010-09-17%"
OR name LIKE "2010-09-16%"
);
or you can use a regex:
SELECT fields
FROM table
WHERE age = "50"
AND name REGEXP "2010-09-17.*|2010-09-16.*";
or, cleverly
SELECT fields
FROM table
WHERE age = "50"
AND name REGEXP "2010-09-1(6|7).*";
There is no combination of the LIKE and IN clauses. It's either one, or the other, syntax:
SELECT fields
FROM table
WHERE age = 50
AND ( name IN ('tim', 'bob', 'nancy', 'john')
OR name LIKE '2010-09-17%'
OR name LIKE '2010-09-16%')
The alternative to consider when searching text is Full Text Search (FTS):
SELECT fields
FROM table
WHERE age = 50
AND MATCH(name) AGAINST('tim bob nancy john')
...but this requires MyISAM tables, and Full Text Indexing.
Put the values in a table (MyParamsTable) and use LIKE in a JOIN condition e.g. something like:
SELECT fields
FROM table
INNER JOIN MyParamsTable
ON table.name LIKE CONCAT(MyParamsTable.name, "%")
WHERE age = "50";