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'
Related
I want to receive info from 2 columns with OR operator, but i can not visualice.
I have my column title and my column categoria. I want to make a select that affects both columns, something like this: selec * from title OR categoria where ...
this is what i have:
final List<Map<String, dynamic>> queryResult = await db
.rawQuery('SELECT * FROM todos WHERE title like ?', ['%'+queryCourse+'%']); //here i want to complete the select with both columns
Use the OR operator for the 2 columns:
final List<Map<String, dynamic>> queryResult = await db
.rawQuery('SELECT * FROM todos WHERE title LIKE ? OR categoria LIKE ?',
['%'+queryCourse+'%', '%'+queryCourse+'%']);
or:
final List<Map<String, dynamic>> queryResult = await db
.rawQuery("SELECT * FROM todos WHERE title LIKE '%' || ? || '%' OR categoria LIKE '%' || ? || '%'",
[queryCourse, queryCourse]);
I assumed that for both columns you will use the same parameter queryCourse.
If not, then change the parameters with the ones you have.
You want the UNION ALL operator.
Select lastName from Employees
UNION ALL
Select lastName from Patients
see
https://www.sqlitetutorial.net/sqlite-union/
Their example is:
SELECT FirstName, LastName, 'Employee' AS Type
FROM employees
UNION
SELECT FirstName, LastName, 'Customer'
FROM customers;
Note, the columns must "match". Here I use "lastName".
You cannot "union all" unlike columns. in the example from the sqlitetutorial, the third column is a "string"... it can have different value(s), BUT it must be the same "data-type" and ordinal position in the SELECT statement.
Be sure to read the documentation on the different between UNION and 'UNION ALL'.
For the "where" condition, you'll have to specify on each "part" of the UNION ALL
https://www.techonthenet.com/sqlite/union_all.php
SELECT department_id, department_name
FROM departments
WHERE department_id >= 10
UNION ALL
SELECT employee_id, last_name
FROM employees
WHERE last_name = 'Anderson'
;
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)
I need to join two queries output of 1st query should be the input for 2nd query in where clause, How do I achieve this?
Select
Distinct
TRNSFR_SRC_ID,
DESCR_ORG,
SUBJECT,
CRSE_NBR,
DESCR1_FRMVW,
SUBJECT_TO,
CATALOG_NBR_TO,
FROM
TRNSFR_CRSE
WHERE
ORG_ID = ?
Select
Distinct
ATTR_VALUE
From TRNSFR_CRSE
Where
ORG_ID = ?
and SUBJECT = ?
and CRSE_NBR = ?
and SUBJECT_TO = ?
and CATALOG_NBR_TO = ?
and CRSE_ATTR = 'GHH'"
Oracle can make this really easy, but I'm not sure how to apply it in this case. But something like this:
select Distinct ATTR_VALUE
from TRNSFR_CRSE
where (ORG_ID, SUBJECT, CRSE_NBR, SUBJECT_TO, CATALOG_NBR_TO) IN
(Select TRNSFR_SRC_ID, SUBJECT, CRSE_NBR, SUBJECT_TO, CATALOG_NBR_TO
from TRNSFR_CRSE
where ORG_ID = ?
) and
CRSE_ATTR = 'GHH'
You can use = rather than in if you know the subquery is supposed to return no more than one row.
If I assume that the output of the 1st query is input to the second query then use below
Select
Distinct
ATTR_VALUE
From TRNSFR_CRSE
Where
(ORG_ID,SUBJECT,CRSE_NBR,SUBJECT_TO,,CATALOG_NBR_TO) in
(Select
DESCR_ORG,
SUBJECT,
CRSE_NBR,
SUBJECT_TO,
CATALOG_NBR_TO,
FROM
TRNSFR_CRSE
WHERE
ORG_ID = ?)
and CRSE_ATTR = 'GHH'"
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))
Would anyone know what to do with this I am doing a like query to select info. example:
SELECT *
FROM customers
WHERE customer_name LIKE '26%'
which will return
26_xx
26_xx
265_xx
but i only want to display 26_xx
I have tried which was suggested from a site :
SELECT *
FROM customers
WHERE customer_name LIKE 'H%!%' escape '!';
but that also returned
26_xx
26_xx
265_xx
In T-SQL you can use [] to escape the wildcard character _;
SELECT * FROM customers
WHERE customer_name LIKE '26[_]%'
The ESCAPE version would be;
SELECT * FROM customers
WHERE customer_name LIKE '26!_%' ESCAPE '!'
SELECT *
FROM customers
WHERE customer_name LIKE '26_%'
Is this what you mean? Only where the names start "26_" or do I read your question wrong?
This should work:
SELECT *
FROM customers
WHERE customer_name LIKE '26[_]%'
and len(customer_name) = 5
Proof:
select
sample_data.*
from
(
select '26_55' as customer_name
union
select '26_abc'
union
select '265_22'
union
select '26-22'
union
select 'abc'
union
select '26_18'
) sample_data
where sample_data.customer_name like '26[_]%'
and len(sample_data.customer_name) = 5