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
Related
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.
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
a, b are not directly related.
What does a,b have to do with the results?
select * from a,b where b.id in (1,2,3)
can you explain sql?
Since you haven't specified a relationship between a and b, this produces a cross product. It's equivalent to:
SELECT *
FROM a
CROSS JOIN b
WHERE b.id IN (1, 2, 3)
It will combine every row in a with the three selected rows from b. If a has 100 rows, the result will be 300 rows.
What you using is Multitable SELECT.
Multitable SELECT (M-SELECT) is similar to the join operation. You
select values from different tables, use WHERE clause to limit the
rows returned and send the resulting single table back to the
originator of the query.
The difference with M-SELECT is that it would return multiply tables
as the result set. For more deatils: https://dev.mysql.com/worklog/task/?id=358
In other word, you query is :
SELECT *
FROM a
CROSS JOIN b
WHERE b.id in (1,2,3)
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.
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,',%')