I'm using oracle 10g and i've a question for you.
Is it possible to "insert" a subquery into a LIKE() operator ?
Exemple : SELECT* FROM users u WHERE u.user_name LIKE ( subquery here );
What i've tried before ->
SELECT * FROM dictionary WHERE TABLE_NAME
LIKE (Select d.TABLE_NAME from dictionary d
where d.COMMENTS LIKE '%table%'
)
WHERE ROWNUM < 100;
It tolds me that my query didnt wokrs -> ORA-00933: la commande SQL ne se termine pas correctement (The sql query doesn't finish correctly) and the last WHERE is out.
I know this is a stupid query, but that just a question that i'm looking for an answer =)
I am guessing you want to do this because you want to compare multiple values at the same time. Using a subquery (as in your example) won't solve that problem.
Here is another approach:
select *
from users u
where exists (<subquery here> where u.user_name like <whatever>)
Or using an explicit join:
select distinct u.*
from users u join
(subquery here
) s
on u.user_name like s.<whatever>
Yeah, why not?
SELECT * FROM users u WHERE u.user_name LIKE (select '%arthur%' from dual);
Example at SQL Fiddle.
IF your subquery return more than 1 row and if this row isn't a string it will not work in your case use IN instead of LIKE
Only if your subquery returns one value
Related
I am using DBeaver to query a PostgreSQL database.
I have this query, it simply selects the highest id per Enterprise_Nbr. The query works but is really slow. Is there any way I can rewrite the query to improve performance.
I am using the querytool DBeaver because I don't have direct access to PostgreSQL. The ultimate goal is to link the PostgreSQL with PowerBi.
select *
from public.address
where "ID" in (select max("ID")
from public.address a
group by "Enterprise_Nbr")
Queries for greatest-n-per-group problems are typically faster if done using Postgres' proprietary distinct on () operator
select distinct on ("Enterprise_Nbr") *
from public.address
order by "Enterprise_Nbr", "ID" desc;
Your query could rewrite as: per each value of Enterprise_Nbr, retrieve row which there is not exists other rows that have same Enterprise_Nbr and greater ID.
SELECT *
FROM public.address a
WHERE NOT EXISTS (
SELECT 1
FROM public.address b
WHERE b.Enterprise_Nbr = a.Enterprise_Nbr AND b.ID > a.ID
)
I have the following SQL query
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE 'A%'
It works as expected. But the following line results in an error message.
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE (select '%arthur%' from dual)
The error message is the following:
ORA-00933 SQL Command not properly ended
I have tried to close the query with ";" but it is still gives the same error. What could cause this error?
Edit: I need LIKE and can't use IN, lets assume we have only one 'arthur' in the users database.
Your query appears to be correct syntactically. The subquery is a scalar subquery, which returns only one column and at most one row.
In general, you can use LIKE with a subquery using EXISTS:
SELECT u.*
FROM users u
WHERE 1 = 1 AND
EXISTS (SELECT 1
FROM t
WHERE u.user_name LIKE t.col
);
If your actual query is more complicated, then this might solve your actual problem.
i have tried same query as per your,it worked fine.
SELECT * FROM employee e where 1=1 and e.name like (select '%abc%' from dual);
I'm getting a HQL error every time I try to run this subquery. This type of query should work in SQL right? How is HQL handling this type of query differently?
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like HQL does not support IN?
I'm getting a error: "Cannot recognize input near 'SELECT'"
In hql if you want to select all fields, you have two ways:
1-You can remove select clause like:
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
or
2-If you want to use select clause, you must use an alias:
SELECT t
FROM Table t
WHERE t.user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like your syntax is incorrect. Try this:
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
There is no "*" in HQL. Only alias should be present.
SELECT t
FROM Table t
HQL supports "In".
Also, make sure, the java class field name is "user_Id". You may be using the DB column name here.
I am using an SQL query with of form
SELECT...FROM...WHERE...GROUP BY id
I want to know how many groups this query returns. How to do this?
SELECT count(id) FROM....WHERE...GROUP BY id
#Tyler Ferraro answers should solve it.
In case the SQL query is very complicated, you can use a nested query:
SELECT COUNT(*) FROM (SELECT...FROM...WHERE...GROUP BY id)
Try this:
select count(*) from (Your SQL).
I need to do
select * from xxx where name in (a,b,c...);
but I want the result set to be in the order of (a,b,c...). is this possible?
I found this question which is looks like your original question: Ordering by the order of values in a SQL IN() clause
ah - I see. you could do something horrendous with a case statement, and then order by that.. you'd effectivley be adding another column to your query to be an "order" that you could then "order by"
its ugly, but if you control the query, and the number in the 'in' clause is low, it could work (beleive an 'in' clause is limited to 255 chars)
e.g "IF name = a then 1 else if name = b then 2"
Failing that, probably best to sort in the client using a similar technique (assuming it was the client that injected the information into the 'in' clause in the first place)
-Ace
The method to do this will be DB-specific.
In Oracle, you could do something like:
SELECT * FROM xxx
where name in (a,b,c...)
ORDER BY DECODE(name,a,1,b,2,c,3);
IN statements are pretty limited, but you could get a similar effect by joining on a subquery.
here's an example:
SELECT x.*
FROM xxx as x
INNER JOIN ((select a as name, 1 as ord)
UNION
(select b as name, 2 as ord)
UNION
(select c as name, 3 as ord)) as t
ON t.name = x.name
ORDER BY t.ord
its pretty ugly, but it should work on just about any sql database. The ord field explicitly allows you to set the ordering of the result. some databases such as SqlServer support a ROWINDEX feature so you may be able to use that to clean it up a bit.