Many selects from different tables and not show result if nothing found - mysql

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.

Related

Value from one table on basis of another table

I have two table as table1 and table2 given below:
I want to have the value of only those table_name from table1 which has there id in print_table column in table 2.
I have implemented the following query but it returns only one value:
SELECT * FROM print_tabel_permission_admin WHERE id IN (select print_table from secondary)
Use FIND_IN_SET:
SELECT DISTINCT
t1.table_name
FROM table1 t1
INNER JOIN table2 t2
ON FIND_IN_SET(t1.id, t2.print_table) > 0;
Demo
You should probably move away from storing CSV data in your tables like this. Instead, break out the IDs in table2 onto separate rows. This will make your life easier.

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

select from table based on select distinct from another table

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
)

Select from table if record found in another table

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
)

How to rewrite this SELECT query

I've been left with some code that looks like this:
SELECT DISTINCT ee.* FROM exp_extensions ee WHERE enabled = 'y'
Our db admin is screaming about the select all, and wants us to grab all the individual fields separately. I've never seen a SELECT DISTINCT * before; how would I rewrite that?
Option 1:
SELECT DISTINCT ee.extension_id, ee.class, ee.method, ee.hook, ee.settings, ee.priority, ee.version, ee.enabled FROM exp_extensions ee WHERE enabled = 'y'
Option 2:
SELECT DISTINCT (ee.extension_id, ee.class, ee.method, ee.hook, ee.settings, ee.priority, ee.version, ee.enabled) FROM exp_extensions ee WHERE enabled = 'y'
Or some other way entirely?
There is no difference between Option 1 or Option 2. Say you have a table that has one column col1 that has one row. All of these produce the same result:
SELECT DISTINCT * FROM t1
SELECT DISTINCT col1 FROM t1
SELECT DISTINCT (col1) FROM t1
SELECT * FROM t1
SELECT col1 FROM t1
SELECT (col1) FROM t1
All the * does is essentially expand to all available columns on the table.