Select Multiple Rows from Joined Table - mysql

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,',%')

Related

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

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

Mysql how to get the records from tableA that dont appear in tableB

i need help on how to get the intersection of two tables in mysql
TABLE A
Col-a
Col-b
TABLE B
Col-a
Col-b
I need all the values in table B that are not in table A
I don't want to use NOT IN because it seems to take over 30 minutes :|
Table A has 35000 records
Table B has 36128 records (but table b is a VIEW from an inner join)
Thanks for helping me.
According to the picture shown here, I'd suggest this right join:
select b.*
from table_a a
right join table_b b
on a.col_a=b.col_a and a.col_b=b.col_b
where a.col_a is null;
Edit: I updated the statement so that now two columns are used for comparision.

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.

MySQL: Using of coma separated ids in a table column and group_concat

In a table, there is a column that contains ids separated by comas ex: "159,167"
I want to do the equivalent of a join between this table and the table that contains those ids.
ex:
TableA TableB
id desc id desc tableAids stock
1 test1 1 abc 1,2,3 1
2 test2 2 def 1,2 0
3 test3 3 ghi 3,4 10
4 test4
How can I join the two tables with a query? I explored the function group_concat() but it returns a string wich I cannot put in a IN clause.
What I am trying to do is something like this:
Select a.desc from TableA a
LEFT JOIN TableB b on a.id = b.tableAids
WHERE b.stock > 1
I have a solution for your question, but:
you should seriously consider a refactoring to get rid of the coma separated ids
the solution does what you need right now, but it cannot be optimised (mysql cannot use indexes to speed-up the execution, see below)
Try this:
select a.desc
from TableA a
inner join TableB b on find_in_set(a.id, b.tableAids) > 0
where b.stock > 1;
Mysql cannot use indexes for joining the tables because the columns in the join condition are included in a function. However, if you have an index on TableB.stock column, it might help a little.
Remember: storing multiple values in a column is a big NO-NO and you should get rid of that asap. It will cause many headaches later.