Sql JOIN request issue - mysql

I have got 2 tables.
A :
ida title
1 aaa
2 bbb
3 ccc
and
B :
idb ida date count
1 1 2014-09-15 14:22:37 15
2 1 2014-09-15 15:52:07 34
3 1 2014-09-15 14:25:38 16
I would like to get all the A table rows, the most recent date and the count corresponding in B even if none of them exist in B table.
I expect this result on this example.
A.ida A.title B.date B.count
1 aaa 2014-09-15 15:52:07 34
2 bbb null null
3 ccc null null
Any help would be apreciated :)
Edit : my initial and false request was :
SELECT e.ida, MAX(n.date_add) as date_notification, n.count FROM ida e LEFT JOIN idb n ON e.ida = n.ida group by e.ida
Don't joining it to this initial Stack was akward.

Here is the query you're looking for:
SELECT A.ida
,A.title
,B.date
,B.count
FROM tableA A
LEFT OUTER JOIN (SELECT B.ida
,MAX(B.date) AS [max_date]
FROM tableB B
GROUP BY B.ida) T ON T.ida = A.ida
LEFT OUTER JOIN tableB B ON B.ida = T.ida
AND B.date = T.max_date
ORDER BY A.ida
The first jointure is used to get only the last date for each ida and the second jointure is used to get the corresponding information from tableB.
Hope this will help you.

Related

What sort of join in mysql should I use to achieve this?

I have two tables:
Table A
id name
--- -----
1 Foo
2 Bar
3 Fred
4 Joe
Table B
id tablea_id result date
--- ---- ---- ---
1 1 A 02/04/2015
2 2 A 02/04/2015
3 1 B 03/04/2015
4 1 C 04/04/2015
How would I be able to achieve a result set that looks like this? What joins would I need in order to 'cycle' what is in table A?
date tablea_id result
--- --- ----
02/04/2015 1 A
02/04/2015 2 A
02/04/2015 3 NULL
02/04/2015 4 NULL
03/04/2015 1 B
03/04/2015 2 NULL
03/04/2015 3 NULL
03/04/2015 4 NULL
04/04/2015 1 C
04/04/2015 2 NULL
04/04/2015 3 NULL
04/04/2015 4 NULL
It looks like you want a cross join to generate all the rows and a left join to bring in existing values:
select d.date, a.tablea_id, b.result
from a cross join
(select distinct date from b) d left join
b
on b.tablea_id = a.tablea_id and b.date = d.date
order by d.date, a.tablea_id;
What joins would I need
An OUTER JOIN and most probably (per your posted expected result) a LEFT OUTER JOIN
select ab.id,ab.date,b.result
from
(select a.id,b.date
from(select distinct id
from table_a)a,
(select distinct date
from table_b)b)ab
left outer join
table_b b
on ab.id=b.id
and ab.date=b.date
Check This Simple Query using Full Outer Join. Live Demo
select distinct a.date,a.id as tablea_id ,result from B b
full outer join
(
select a.id,b.date from B b,A a
)A on a.id=b.tablea_id and a.date=b.date

MySQL join 2 tables and unite a column

I have two tables:
Table A
ID | DATE | VALUE | KEY|
1 30.8.14 100 11
2 25.8.14 500 11
2 20.8.14 250 11
Table B
ID | DATE | VALUE | KEY|
1 30.8.14 AB 11
2 25.8.14 CD 11
3 10.8.14 EF 11
These two tables should be merged, key is used to define which entries should be merged WHERE KEY = '11'
IF there is a date in TABLE A that is also in TABLE B, it becomes on entry with both values
IF there is no date in TABLE A that is also in TABLE B, the value for B becomes (null)
And in the End, there should be only 1 date field.
Columns should also be be a unique name..
I created this example table, how my output should look like
joinedDate | aValue | bValue
30.8.14 100 AB
25.8.14 500 CD
20.8.14 250 (null)
10.8.14 (null) EF
Im using MySQL version 5.5 on Maria DB
Could someone help me here?
You seem to want full outer join, which MySQL doesn't offer. Here is one method:
select d.date, a.value as avalue, b.value as bvalue
from ((select date from a union
select date from b
)
) d left join
a
on a.date = d.date left join
b
on b.date = d.date;
select a.date, a.value as avalue, b.value as bvalue
from tablea a
left join tableb b
on a.date = b.date
union all
select b.date, null, b.value
from tableb b
left join tablea a
on a.date = b.date
where a.date is null
Fiddle:
http://sqlfiddle.com/#!2/09ab9e8/4/0

MYSQL query to match multiple values from two tables

I'm having trouble fixing a MYSQL query that will find "matches" between 2 relational tables. For example, if the TABLEA ID being passed in was 2, then I want to return all IDs from TABLEB where both TABLEA VALUES (b and c) exist for a single TABLEB ID. Hope that made sense! In this instance it wouldn't return anything. However, when passing in ID 3 it would return 13 as d and e exist within ID 13 rows in TABLEB. Any help massively appreciated!
TABLEA
ID | VALUE
1 | a
2 | b
2 | c
3 | d
3 | e
TABLEB
ID | VALUE
10 | a
12 | b
12 | z
13 | d
13 | e
13 | f
You can try this query.
UPDATE:
In MySQL :
SELECT tab_b.ID
FROM TABLEB tab_b
WHERE tab_b.VALUE IN (SELECT TABLEA.VALUE
FROM TABLEA
WHERE TABLEA.ID = 3)
GROUP BY tab_b.ID
HAVING COUNT(DISTINCT tab_b.VALUE) = (SELECT COUNT(TABLEA.VALUE)
FROM TABLEA
WHERE TABLEA.ID = 3)
Check this Fiddle in MySQL: ->http://sqlfiddle.com/#!2/b1aba/10
Fiddle in SQL Server: -> http://sqlfiddle.com/#!3/b0d63/3
(You'll have to change the values from 2 to 3 to see the desired result)
Hope this helps!!!
select b.id2
from tableA a
left join tableB b
on a.value = b.value2
where a.id = 2
and id2 is not null
hope this answers it for you
here is the fiddle
fiddle
http://sqlfiddle.com/#!3/7e4b1/5

Fetch DISTINCT records with JOIN of two tables

I have two tables:
builders
b_id fk_c_id
1 1
2 1
3 1
4 1
5 1
6 2
7 2
subbuilders
fk_b_id sb_id
1 2
1 3
1 4
2 5
6 7
and i want Distinct b_id which are not exist in subbuilders table and must have same fk_c_id
I create:
SELECT DISTINCT b.id FROM pms_builder_to_subbuilders bsb
LEFT JOIN pms_builders b ON b.id = bsb.sb_id WHERE b.fk_c_id = '1'
but it show Distinct records from subbuilders.
You can get the desired results with the following query:
SELECT DISTINCT b.b_id FROM builders b LEFT JOIN subbuilders sb ON sb.fk_b_id = b.b_id WHERE b.fk_c_id = '1' AND ISNULL(fk_b_id);
i think you want this query:
SELECT DISTINCT b_ID
FROM builders
WHERE b_ID NOT IN
(SELECT DISTINCT fk_b_id FROM subbuilders)
but it returns
b_ID
========
3
4
5
7
but i didn't understand your statement: must have same fk_c_id. What do you mean by that?
b_id = fk_c_id
if that's the case then there will be no rows returned because only record 1 has the same b_ID and fk_c_id but exists in table subbuilders

mysql two table query problem

a table
a_id a_value
1 text1
2 test2
b table
b_id b_num a_id
1 5 1
2 7 1
3 2 1
4 7 2
5 56 2
Results base a table (edited)
a_id:1 a_value:text1 total:3 records
a_id:2 a_value:text2 total:2 records
How can get this format in sql?
query a table and add a field(total) count b.a_id = a.a_id in table b
thank you..
You can try:
SELECT a.a_id AS id, a.a_value AS value, (SELECT count(b.b_id) AS count FROM b WHERE (b.a_id = a.a_id)) AS total FROM a GROUP BY a.a_id
Then the result for your example using the data from tables a and b:
**id value total**
1 text1 3
2 text2 2
I imagine you have an error in your b table, so I will assume what you call b_id is actually a_id, or your results would be wrong
Anyway you could use:
SELECT COUNT(b.a_id) AS total FROM b GROUP BY (SELECT a.a_id FROM a)
ORDER BY b.a_id
The updated query based on changes to the question
SELECT a_id, a_value, x.total
FROM a
INNER JOIN
(SELECT b.a_id, COUNT(1) AS total
FROM b
GROUP BY (b.a_id)) X
ON a.a_id = x.a_id
ORDER BY a.a_id