select to check if a column does not contain a value in a provided array - sql

I would like to select to check if any of the countries I have in my string array is not contained in the country table in the psql database.
Therefore I have a list of country names ARRAY['Country1','country2'.....]
and I have a table of country and I want a query to select countries that are not in this string that I will provide to the where clause
Something like this
SELECT name from country where name not in (ARRAY['Country1','country2'.....])

You were nearly there:
SELECT name
from country
where name <> ALL (ARRAY['Country1','country2'.....])

Related

How can I bring a register only if it doesn't contains a string - SQL?

My query is something like this:
SELECT * FROM table WHERE name LIKE "%Mary%"
But now, I want bring all the occurrences as long as it's not contains "Jane" especially in "Mary". I do not want "Mary Jane", neither "Jane Mary" or any variation (e.g. "Mary Smith Jane").
I really don't know how to.
EDIT:
I'm not sure if I can only use a "not like" because I'm already using "not like" in the same query for other reasons.
In fact:
SELECT * FROM table WHERE name NOT LIKE "%John%"
AND name NOT LIKE '%Charlie%'
AND name LIKE '%Mary%'
Just add that to the WHERE clause:
WHERE name LIKE '%Mary%' AND
name NOT LIKE '%Mary Jane%'
Or, if you mean that the exact match is not what you want:
WHERE name LIKE '%Mary%' AND
name <> 'Mary Jane'
SELECT *
FROM your_table
WHERE name LIKE '%Mary%'
and name <> 'Mary Jane'
In order to retrieve records that contain Mary in the string, but not Jane, you will want to keep your clause for LIKE '%Mary%' and add a clause for NOT LIKE '%Jane%'.
You can group these clauses together with parenthesis in order to isolate them from other clauses in the query.
SELECT *
FROM table
WHERE
(name LIKE '%Mary%'
AND name NOT LIKE '%Jane%')

selecting with a column being one of two possible values

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

How to change Select List values based on other Select List value using oracle apex?

I have two select lists in my page, Region and Country.
Country should display values based on the value selected from the Region list. But the problem is, it's displaying blank in the Country list, even after selecting a value from the Region list.
Region table Country table
Id Group_Id
Name Name
Id
For Region, I have written the query as follows:
Select Name d,Id r from Regions
The query for Country as:
Select Name d,Id r from Country where Group_Id = :P3_REGION
How can I get the correct values?
P3_REGION should be entered as Cascading LOV Parent Item(s) of the Country SelectList.

SQL using where contains to return rows based on the content of another table

I need some help:
I have a table called Countries, which has a column named Town and a column named Country.
Then I have table named Stores, which has several columns (it is a very badly set up table) but the ones that are important are the columns named Address1 and Address2.
I want to return all of the rows in Stores where Address1 and Address2 contains the towns in the Countries table.
I have a feeling this is a simple solution but I just can't see it.
It would help if maybe you could use WHERE CONTAINS but in your parameters search in another table's column?
e.g.
SELECT *
FROM Stores
WHERE CONTAINS (Address1, 'Select Towns from Countries')
but obviously that is not possible, is there a simple solution for this?
You're close
SELECT * FROM Stores s
WHERE EXISTS (
SELECT * FROM Countries
WHERE CONTAINS(s.Address1, Town) OR CONTAINS(s.Address2, Town)
)
This would be my first attempt:
select * from stores s
where
exists
(
select 1 from countries c
where s.Address1 + s.Address2 like '%'+c.Town+'%'
)
Edit: Ooops just saw that you want the 'CONTAINS' clause. Then take Paul's solution

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)