SQL Selecting from a value from two tables - mysql

I have the following SQL:
SELECT * FROM `table` WHERE `code` = 15510642
I want to modify so that it checks another table as well, so for example:
SELECT * FROM `table`,`table2` WHERE `code` = 15510642
However that doesn't work. Please help!

Perhaps the poster means UNION because he wants results from both tables?
SELECT * FROM `table` WHERE `code` = 15510642
UNION [ALL]
SELECT * FROM `table2` WHERE `code` = 15510642
Works only when both tables contains same column (or specify them instead *)

you'll have to join the tables if there's a relation between them.
select * from table as t1, table2 as t2 where t1.code = 15510642 or t2.code=15510642
and t1.id = t2.foreignkeyid
If there's no relation you could try a union, but the fields must match. So only use fields from both tables that match.
select id, somefield, somefield2 from table1 where code = 15510642
union
select id, somefield, somefield2 from table2 where code = 15510642

Work with an inner join.
It will be something like
SELECT *
FROM Table t INNER JOIN table2 t2
ON t.Code = t2.Code
WHERE t.Code = 15510642
I hope this helps!
Tjeu

Related

SQL Union two tables without relation and return separate columns

I have to tables 'Table1, Table2' which are not related at all, but I need to do a common query with both because a filter.
I thought about this solution:
select * from(
select t1.idT1 from Table1 t1 where idT1 = 1
union all
select t2.idT2 from Table2 t2 where idT2 = 1) as results
but it returns me a single column named idT1 and what I need is two separated columns: 'idT1', 'idT2' because I need to know if id is from table 1 or table 2 to look for their details later.
Is it possible?
select * from(
select t1.idT1, null as IdT2 from Table1 t1 where idT1 = 1
union all
select null as idT2, t2.idT2 from Table2 t2 where idT2 = 1) as results
or as #jarlh suggested
select * from(
select 't1' as T1OrT2, t1.idT1 from Table1 t1 where idT1 = 1
union all
select 't2' as T1OrT2, t2.idT2 from Table2 t2 where idT2 = 1) as results
I am assuming that where IdT1=1 in your question is just an example, because if it is really that then the result will be all 1s, as #Nathan_Sav pointed out.

Select distinct rows based on column and two conditions in SQL

I would like to retrieve rows from this data set where a T1/T3 value exists, but no T2/T3 value exists for a corresponding ID.
ID sample1 sample2 value
A_000002 T2 T3 -0.934119
A_000002 T1 T3 -0.866637
A_000029 T2 T3 -1.07677
A_000037 T2 T3 -0.76506
A_000057 T1 T3 -5.34988
I'd like to say something like:
SELECT * FROM table
WHERE DISTINCT ID
AND sample_1 == "T1"
AND sample_2 == "T3"
...and return only the following because it has no corresponding T2/T3 row for that ID:
A_000057 T1 T3 -5.34988
If I use sample_1 and sample_2 conditions, I get distinct values anyway because it filters out the the "T2" values before checking if the ID is distinct.
The closest I've come is to make 3 tables with the possible T1/T2/T3 combinations and screen for NOT EXISTS T1T2.ID = T2T3.ID
select * from T1T2
where not exists (select * from T2T3 where T2T3.id = T1T2.id)
and not exists (select * from T1T3 where T1T3._id = T1T2.id)
order by id
I'm not sure I trust the code yet though.
You can use not exists :
select t.*
from table t
where (sample1 = 'T1' and sample2 = 'T3') and
not exists (select 1
from table t1
where t1.id = t.id and
t1.sample1 = 'T2' and t1.sample2 = 'T3'
);
You can use this technique based on an outer join:
select t1.*
from table t1
left join table t2
on t2.id = t1.id
and t2.sample1 = 'T2' and t2.sample2 = 'T3'
where t1.sample1 = 'T1' and t1.sample2 = 'T3'
and t2.id is null
It works because outer joins return nulls if there's no join, and you return only those via the t2.id is null condition in the where clause.
The big advantage of this technique is the efficient use of the index on the id column; this technique will generally out-perform other apporaches. Plus IMHO it's a neater looking query.
Note that the condition for the sample columns must be in the join condition for t2, otherwise you'd effectively get an inner join, defeating the required outer join.
I would use not exists:
SELECT t.*
FROM table t
WHERE t.sample_1 = 'T1' AND t.sample_2 = 'T3' AND
NOT EXISTS (SELECT 1 FROM table t2 WHERE t2.id = t.id);
Here will surly work:
Select distinct ID, * from table
Where sample1 = 't1' and sample2 = 't3'

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
)

How to perform subquery where subquery returns more than one row?

I'm trying to search for all entries in one table where they have a column that matches entries in another column that precede a -
Example Output:
This is the query I tried, it returned the error of "Error in query (1242): Subquery returns more than 1 row"
SELECT * FROM table1
WHERE
table1.Column1 = (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
You can use IN in your WHERE clause :
SELECT * FROM table1
WHERE
table1.Column1 IN (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
Another way is to use JOIN as
SELECT * FROM table1 t1
inner join (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1) as str
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
)t2
on
t1.column1 = t2.str
;
DEMO

How to subselect based on multiple column key

I'm working with a legacy database that uses a three column key for products. I want to select all products that have a status of 'A' or that have a matching record in a second table. If it were a single column primary key (like 'id'), I would do it this way:
SELECT * FROM `product`
WHERE `status` = 'A'
OR `id` IN (SELECT `foreign_key` FROM `table2`)
I can't figure out how to do the IN-clause subselect with three keys though. I suppose I can concatenate the keys together and compare the strings, but that seems horribly inefficient. Is there a way to do this without concatenation?
You can LEFT JOIN table product and table2 on the composite key, then status = 'A' OR table2.id IS NOT NULL
A LEFT [OUTER] JOIN can be faster than an equivalent subquery because the server might be able to optimize it better
SELECT * FROM product p1
WHERE status = 'A'
OR EXISTS (SELECT *
FROM table2 t2
WHERE t2.id = p1.foreign_key
AND t2.other_key = p1.secret_key
...
);
Do a left join :)
SELECT p.* FROM product p
LEFT JOIN table2 t2 on p.key1 = t2.key1 and p.key2 = t2.key2 and p.key3 = t2.key3
WHERE status = 'A' OR t2.key1 IS NOT NULL
You could use a UNION:
SELECT *
FROM 'product'
WHERE 'status' = 'A'
UNION
SELECT *
FROM 'product'
JOIN 'table2'
ON (product.id = table2.foreign_key
AND ...)