How to compare two seperate tables based on one common field? - mysql

I have two tables containing the same datasets but from 2 diffrent sources. I want to compare them by the "price" field.
I want to get a combined table but showing me only the results where both IDs are the same and the prize of table B is lower then the price of table A.
Here is an example of what I want. https://i.imgur.com/h2eq5TD.png
Is this possible in SQL or do I need to do this manually with for example python.

This sounds like a join:
select a.*, b.*
from a join
b
on a.id = b.id and b.price < a.price

Related

Generate list of distinct dimensions in a table

I have a table with 3 dimensions- A, B, and C.
I essentially want values of all possible combinations for these dimensions and populate all measures(M) as 0 when a combination isn't present.
Suppose I have the table-
If I do this I get -
select a,b,c from sum(m) fact group by a,b,c
But I would like all possible combinations, -
Currently, I a doing a cross join like below, but is there some faster way to do this (as my table has about ~1M records)? -
select * from (
select distinct f1.a, f2.b, f3.c
from fact f1
cross join fact f2
cross join fact f3 ) all
left join
( select a,b,c from sum(m) fact group by a,b,c) s
on all.a=s.a and all.b=s.b and all.c=s.c
If this is Oracle Database, then this is exactly what cube is for.
select a, b, c, sum(m)
from my_table
group by cube(a,b,c)
MySQL:
GROUP BY a,b,c will produce 1 row per combination that exists in the table.
If you want all possible combinations, you need to build 1 (or 3) more tables to list all the possible values, then do a LEFT JOIN from them. You may also want COALESCE(col, 0) to turn NULLs into zeros.

SELECT all values from one table with reference to another table but the value there is missing

I have two tables (in reality I have multiple tables but for this problem I need only two). I want to select ALL values from one table where a condition is met. The condition is - select all those values which are not referenced in the second table. To be more clear: imagine you have one table with your friends name, address, and phone number. They were also meant to give you their birthday, which you want to store in the second table. Now you want to find all those who have NOT given you their birthday yet. So you want to list all the information from one table where the values related to them in the second table is null.
I have tried the following:
SELECT A.value1, A.value2, A.value3
FROM Table A, Table B
WHERE A.PrimaryKey=B.PrimaryKey
AND B.value1 = NULL;
You are using implicit inner join syntax, when you need a left join:
SELECT A.value1, A.value2, A.value3
FROM TableA A
LEFT JOIN TableB B
ON A.PrimaryKey=B.PrimaryKey
WHERE B.value1 IS NULL;
Use left join for your query as following :
SELECT A.value1, A.value2, A.value3
FROM A
LEFT JOIN B
ON A.PrimaryKey=B.PrimaryKey
WHERE B.value1 IS NULL;

join two different tables?

How can I join table a and table b and get records for each? Not an actual join... not sure what this is called.
So if I have 3 records in a, and 5 records in b, I want 8 records back.
In a record for a, all b fields can be null. In a record for b, all a fields can be null.
edit: My tables have different fields.
Error Code: 1222. The used SELECT statements have a different number of columns
Like the others mentionned, you need an union
SELECT intColumn, varcharColumn, intColumn FROM a
UNION
SELECT intColumn, varcharColumn, 0 FROM b
but you must have the same number of columns and they must also have similar data types.
Here's a good tutorial about it
Also, if you want columns that are not in both tables, you can fill with nulls or constants.
You want a UNION:
SELECT something FROM a
UNION
SELECT something FROM b
Try this
SELECT * FROM a
LEFT JOIN b ON a.id1 = b.id2
UNION
SELECT * FROM a
RIGHT JOIN b ON a.id1 = b.id2
Just make sure, that A and B have different IDs
Edit: Working Fiddle
You can also use some other field other then id which are not same in two table
Edit: Updated fiddle

Transforming a Complicated Requirement into a SQL Query

I am having trouble with the relational algebra and transformation into SQL of this rather complicated query:
I need to select all values from table A joined to table B where there are no matching records in table B, or there are matching records but the set of matching records do not have a field that contains one of 4 of a possible 8 total values.
Database is MySQL 5.0... using an InnoDB engine for the tables.
Select
a.*
from
a
left join
b
on
a.id=b.id
where
b.id is null
or
b.field1 not in ("value1","value2","value3","value4");
I'm not sure if there is any real performance improvement but one other way is:
SELECT
*
FROM
tableA
WHERE
id NOT IN ( SELECT id FROM tableB WHERE field1 NOT IN ("value1", "value2"));
Your requirements are a bit unclear. My 1st interpretation is that you only want the A columns, and never more than 1 instance of a given A row.
select * from A where not exists (
select B.id
from B
where B.id=A.id
and B.field in ('badVal1','badVal2','badVal3','badVal4')
)
My 2nd interpretation is you want all columns from (A outer joined to B), with perhaps more than one instance of an A row if there are multiple B rows, as long as not exists B row with forbidden value.
select * from A
left outer join B on A.id=B.id
where not exists (
select C.id
from B as C
where A.id=C.id
and C.field in ('badVal1','badVal2','badVal3','badVal4')
)
Both queries could be expressed using NOT IN instead of correlated NOT EXISTS. Its hard to know which would be faster without knowing the data.

Select Multiple Rows from Joined Table

I have a table A with a field X that contains a value list like 1,2,3
Then there is a second Table B with a field uid.
If A.X contains 1,2,3 i want to get the rows from table B where uid is 1 or 2 or 3.
I tried a Join like:
SELECT b.value FROM A a JOIN B b ON b.uid IN ( a.X )
This kinda works but i only get 1 Result in b.value of course. How can I get all the Results? I know i could just use a second Query, but it is possible to get this in one?
(I know it is not nice to use such a structure and one should use n-n tables, but this is given by the used System)
As you state in your question this is not a good design. I think the following should work (albeit not with good performance)
SELECT b.value
FROM A a
JOIN B b ON CONCAT(',',a.X,',') LIKE CONCAT('%,',b.uid,',%')