I need to select some rows from Table 1 lets say if a value is found in Table 2. So I want to check if the value (I will enter the value from command line) is found in Table 2 and then select rows from Table1, if not I want to select rows from another table.
I tried CASE but from what I got that works only if you want to check for value within one table. Any idea?
You can do something like this:
-- If value is found in table2, select from table1
select * -- <- use padding if necessary
from table1
where exists (select 1
from table2
where myField = value)
union all
-- If value is not found in table2, select from another_Table
select * -- <- use padding if necessary
from another_Table
where not exists (select 1
from table2
where myField = value)
This query will select from Table1 if :id exists in Table3, and from Table2 otherwise:
select *
from Table1
where exists
(
select *
from Table3
where id = :id
)
union all
select *
from Table2
where not exists
(
select *
from Table3
where id = :id
)
Related
I am not able to return values for below query. The aim of this is I need to return the values depending on id.
oid are in form 123, 123.456, 123.456.789 . So if the result of SELECT id FROM table1 where name = 'Test999'is 456. it should return 2 columns i.e., 123.456, 123.456.789 . I am trying to give a % symbol before the query seems like some syntax error. Tried giving double quotes still no progress.
SELECT * from table1 where oid like
%(SELECT id FROM table1 where name = 'Test999')
I could manage it by adding a percent symbol to the output column of the subquery
select *
from (select 'aa' q union all select 'bb') some_table
where some_table.q like (select '%a')
Thus, your query must look something like that
SELECT * from table1 where oid like
(SELECT '%'||id FROM table1 where name = 'Test999')
What you must be aware of is query will fail whenever subquery returns more than 1 row
UPD: I'm dumb. You just need to eliminate the subquery:
SELECT *
from table1 t1
join table1 t2
on t1.oid like concat('%', t2.id)
You can use exists:
SELECT t1.*
FROM table1 t1
WHERE EXISTS (SELECT 1
FROM table1 tt1
WHERE t1.oid LIKE CONCAT('%', tt1.id, '%') AND
tt1.name = 'Test999'
);
I have a query with SQL as it needs an expertise. I have two tables Table A and B. Now I need to retrieve results from table 1 based on some conditions and i also need to retrieve results from table 1 based on results from table 2.
I want to achieve
Select * from table1 where author ="xyz") + select * from table1 where id=""
--->id = select post_ID from table2 where author = "abc"
So the ID values of table1 matches the post_ID values of table 2
You could use OR condition
select t1.* from table1 t1 where author ='xyz'
or exists ( select 1 from table2 t2 where t2.post_ID=t1.id)
Try UNION to merge the results and IN to compare with post_ID value of table 2. Below code might help you.
Select * from table1 where author ="xyz"
UNION
select * from table1 where id IN (select post_ID from table2 where author = "abc")
I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".
the case is that I need to select a field distinct from table1 (no duplicates) and use the result as a key to select from another table2. And I need this to be in one query. Is this possible?!
table1: hID, hName, hLocation
table2: hID, hFrom, hTo, hRate, hRoomType, hMeals
I want to correct version of this query:
SELECT
*
FROM
table1
JOIN (
DISTINCT
hID
FROM
table2
WHERE
hRoomType = Double Room
ON table1.hID = table2.hID)
expected result: all hotels that offer Double Room thanks much –
thanks for help!
Your question is quite vague and confusing. Is this what you are looking for:
SELECT hID, name, location
FROM table2
INNER JOIN table1
ON table1.hID = table2.hID
GROUP BY table2.hID;
Here is a skeleton to achieve this:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM (
-- This is the distinct list from table1
SELECT DISTINCT
id
FROM
table1 T1
) DT1
INNER JOIN table2 T2
ON T1.id = T2.reference_to_t1_id
Another solution if you don't want to retrieve any columns from table1:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM
table2 T2
WHERE
-- Sais that get all record from table2 where this condition matches
-- at least one record
EXISTS (
SELECT 1 FROM table1 T1 WHERE T1.id = T2.reference_to_t1_id
)
For your tables and question
SELECT
hID, hName, hLocation
FROM
table1 T1
WHERE
EXISTS (
SELECT 1 FROM
table2 T2
WHERE
T1.hID = T2.hID
AND T.hRoomType = 'Double' -- Assuming that this is the definition of double rooms
)
I have many tables and some of them have column 'Item'. I want to make easy query,which allow me to check in which table or tables i can find item with number which i want.
I have read some info about variable and made this:
SET #Item_id =(there i put number) ;
select * from table1 where item=#Item_id;
select * from table2 where item=#Item_id;
select * from table3 where item=#Item_id;
select * ...(other tables)
Problem is my DBMS will show tabs with result for each table and it doesn't matter if search was successful or not.
How can i update code to show me tables only with results ?
SET #Item_id = 29434;
SELECT
(select distinct item from creature_loot_template where item=#Item_id) as table1
(select distinct item from gameobject_loot_template where item=#Item_id) as table2
(select distinct item from item_loot_template where item=#Item_id) as table3
This may not do exactly what you want, but for instances like this, I usually use the union option to dump the results from all the queries into one single table. To show which query or source table it came from, I make a fake column with a string constant.
SET #Item_id =(there i put number) ;
select 'table1', * from table1 where item=#Item_id union
select 'table2', * from table2 where item=#Item_id union
select 'table3', * from table3 where item=#Item_id;
You can try Something like this:
SET #Item_id =(the Id you want to check) ;
SELECT
(select distinct item from table1 where item=#Item_id) as table1
(select distinct item from table2 where item=#Item_id) as table2
(select distinct item from table3 where item=#Item_id) as table3
...
That way, you should have an input like this:
table1 table2 table3
null null item
Therefore you will know in which table you found your item.