Hi I am writing oracle query to support select all for in clause my query goes something like this
SELECT * FROM country
WHERE
country_id in( IF('test' = 'test',(1,2,3),true) )
If condition ('test' = 'test') is true then it should fire query like
SELECT * FROM country WHERE country_id in(1,2,3)
Else it should fire query
SELECT * FROM country WHERE country_id in(true)
If I understand correctly what you need you will have to split it into 2 conditions:
SELECT *
FROM country
WHERE(('test'='test')AND(country_id IN (1,2,3)))
OR(('test'<>'test')AND(country_id<>0))
Related
With this query:
SELECT *
FROM table1
WHERE name = 'Peter'
I can retrieve all data from Peter from table1. This can be done with the "Wildcard *".
Question
Is there any kind of wildcard for the WHERE part? For example:
SELECT *
FROM table1
WHERE name = *
This option of course not working, but I am looking for a wild card there so that all names will be included in my query. I know it's easier to remove the WHERE statement, but due to some reasons I still need it.
SELECT *
FROM table1
WHERE True OR name = 'Peter'
;
This may look silly, but it can come in handy when generating query strings, eg in PHP/PDO:
$query = "SELECT * FROM names
WHERE ($ignore_name OR name = :the_name)
AND ($ignore_address OR address LIKE :the_address)";
, where the $ignore_xxx variables are either True or False, and completely under your control (not user-input!)
select *
from table1
where name = 'Peter' or name = name;
You can query output into your WHERE clause like so:
SELECT *
FROM table1
WHERE [name] IN (SELECT DISTINCT [name]
FROM table1)
My table looks something like this:
I want to retrieve all the PractitionerIdFK if they have SpecialityIdFK = 1 AND SpecialityIdFK= 2. I tried the following but it doesn't seem to work.
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK IN (
SELECT PractitionerSpecialities.SpecialityIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK = 1
AND PractitionerSpecialities.SpecialityIdFK = 2
)
You can use GROUP BY and HAVING:
SELECT ps.PractitionerIdFK
FROM PractitionerSpecialities ps
WHERE ps.SpecialityIdFK IN (1, 2)
GROUP BY ps.PractitionerIdFK
HAVING COUNT(*) = 2; -- the size of the comparison list
This assumes that there are no duplicates in PractitionerSpecialities. If that is a possibility, then use HAVING COUNT(DISTINCT ps.SpecialityIdFK) = 2.
It can be achieved by using IN and BETWEEN operator in SQL .
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK in (1,2)
-- You can BETWEEN Clause as well ..
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK BETWEEN 1 AND 2
In Sub query use OR operator instead of AND .
hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
In SQL Server I had the very convenient ability to make a query like this:
SELECT phone_number, last_known_location, *
FROM missing_female_pilots
WHERE last_name = 'Earhart'
How can I do something similar in Oracle?
You can use table alias :
SELECT t.phone_number, t.last_known_location, t.*
FROM missing_female_pilots t WHERE t.last_name = 'Earhart'
Or just prepend table name before * :
SELECT phone_number, last_known_location, missing_female_pilots.*
FROM missing_female_pilots WHERE last_name = 'Earhart'
I have a SQL statement that I want to return 6 fields found from a MINUS statement that only compares 1 field from 2 tables. It works properly when the MINUS statement only returns 1 entry, but errors if it returns more than 1.
SELECT DROPPER_ID, EMAIL, ACTIVE, COUNTRY_CD, FIRST_NAME, LAST_NAME FROM PETE.DROPPER
WHERE DROPPER_ID = (
SELECT DROPPER_ID FROM PETE.DROPPER WHERE COUNTRY_CD <> 'USA' AND ACTIVE = 1
MINUS
SELECT DROPPER_ID FROM PETE.DROPPER_COMPARE);
How can I accomplish this?
Instead of using =, try using the IN statement:
SELECT DROPPER_ID, EMAIL, ACTIVE, COUNTRY_CD, FIRST_NAME, LAST_NAME FROM PETE.DROPPER
WHERE DROPPER_ID IN (
SELECT DROPPER_ID FROM PETE.DROPPER WHERE COUNTRY_CD <> 'USA' AND ACTIVE = 1
MINUS
SELECT DROPPER_ID FROM PETE.DROPPER_COMPARE);
The problem is in your where clause: it is currently designed to compare dropper_id with a single value, but that's not actually what you want. To fix it, replace the '=' symbol with the word 'in'. This will tell the WHERE clause to treat the subquery as a tuple or list of values instead of a single value.