mysql two table query problem - mysql

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

Related

Select date closest to date in another table

I have two tables, one having a entry date, and the other with an effective date. What I need to do is select the row were the entrydate is closest to the effective date. The only resource I can find is row_number() which does not seem to work in MySQL.
data
Table A Table B
id effdate id Aid entrydate
1 2015-10-19 1 1 2015-12-17
2 1 2015-12-18
3 1 2015-12-20
What I am trying to do is select
id effdate entrydate
1 2015-10-19 2015-12-17
So far I have tried using min() on entrydate, but it will just time out.
SELECT a.id, a.effdate, b.entrydate
FROM tableA a
JOIN tableB b on a.id = b.Aid
SELECT a.id, a.effdate, b.entrydate
FROM tableA a
JOIN tableB b on a.id = b.Aid
ORDER BY DATEDIFF(entrydate, effdate) ASC
-- you might want to order here by additional fields to break the ties
LIMIT 1;
If entry date is always greater than the effective date you can use the following
select a.id, a.effdate, b.entrydate from aa a, bb b
where a.id = b.aid
and b.entrydate = (select Min(bi.entrydate)
from bb bi
where bi.id = a.id
);

Query to find the sum of the values for duplicate id in MySQL

I have a requirement :
ID Quantity
1 5
1 4
1 2
1 4
2 1
2 2
2 3
3 1
3 3
3 2
Output required :
ID Quantity
1 15
2 6
3 6
I have tried the below query on MySQL but unable to get appropriate result.
SELECT
a.id, sum(b.Quantity)
FROM
​table_name a
INNER JOIN
​table_name b
ON a.id = b.id
​ ​group by
a.id, b.Quantity
Just remove ​b.Quantity from ​group by statement. SQL fiddle
SELECT
a.id, sum(b.Quantity)
FROM
​table_name a
INNER JOIN
​table_name b
ON a.id = b.id
​ ​group by
a.id
http://sqlfiddle.com/#!9/003625/1
SELECT id, SUM(quantity)
FROM `table_name`
GROUP BY id
You don't need any join. Simply try:
select id,sum(quantity) from table_name group by id
Use GROUP BY clause along with sum() like the query below-
select ID, sum(Quantity) FROM table_name GROUP BY ID;

How to fetch matched data from first table and unmatched from second table

I have 2 tables with same structure
eg. Table A and B with columns id,value.
Table A
ID VALUE
1 10
2 20
3 0
Table B
ID VALUE
1 24
2 26
3 0
4 40
5 50
expected output:
ID VALUE
1 10
2 20
3 0
4 40
5 50
as per output first three id is matched with table B so id(1,2,3) with value comes from table A and id(4,5) with value is not matched so it comes from table B.
You could use a right join on table b (or a left join on table a) and use a coalesce operator.
select b.id, coalesce(a.value, b.value)
from tablea a
right join tableb b on a.id = b.id
or
select b.id, coalesce(a.value, b.value)
from tableb b
left join tablea a on a.id = b.id
see SqlFiddle

Joining two tables on interval basis

In SQL, suppose that I have table A
ID
--
1
3
5
and table B
ID2
---
1
2
3
4
5
6
To get the result similar to:
ID | ID2
----------
1 | 1
1 | 2
3 | 3
3 | 4
5 | 5
5 | 6
For an explanation, an element in column ID2 will be mapped to the highest value in the column ID that is less than or equal to the said element in ID2.
For example, 4 in column ID2 is mapped to 3 from column ID, because 3 is the largest value in column ID which is less than or equal to 4.
Is it possible at all to do this in sql?
What I would do is start by joining the two tables on the condition that the id in the first table is less than or equal to that in the second table, like this:
SELECT t1.id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id;
Once you have that, you can select the maximum id from the first table, and group by the id from the second table to get the largest pairs:
SELECT MAX(t1.id) AS id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id
GROUP BY t2.id;
SQL Fiddle seems to be down but I will update with a link as soon as I can.
SELECT MAX(A.ID) ID, B.ID2
FROM A
INNER JOIN B ON B.ID2 >= A.ID
GROUP BY B.ID2
If you only need the matching ID column:
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
If you need more columns:
select *
from a
join
(
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
) as b
on a.Id = b.a_Id

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