How to select the ids present in another table multiple columns? - mysql

Suppose I have two tables TableA and TableB having structure as shown below:-
**TableA**
id | col_1 | col_2
1 | A | B
2 | C | D
3 | E | F
4 | G | H
**TableB**
id | TableA_first_col_id | TableA_second_col_id
1 | 1 | 2
2 | 1 | 3
Now, I want to check if TableA id is present in TableB TableA_first_col_id column or in TableA_second_col_id column.
Result should come to be
**TableA.id**
1
2
3
How can I write an optimised mysql query for this?

Try below ;
Select A.id
From TableA A
Where A.id in (select TableA_first_col_id From TableB) OR
A.id in (select TableA_second_col_id from TableB)

select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)

select id from tableA where exists
( select id
from tableB
where tableA.id = tableB.first_col_id or tableA.id = tableB.second_col_id
)

Related

Compare data for 2 tables

i have 2 tables A and B like below
Table A
+---------+--------+
| query | status |
+---------+--------+
| number1 | solved |
+---------+--------+
table B
+----+---------+---------+
| id | query | status |
+----+---------+---------+
| 1 | number1 | started |
| 2 | number1 | working |
| 3 | number1 | solved |
+----+---------+---------+
how do i check whether the latest status of table B is equal to status of Table A
i have tried first getting latest status for table 2 but i am unable to get so
select number ,max(status),max(id)
from Table B
group by number
order by max(id) asc
Here is a simple solution relying on sorting by ID and getting only first row in a sub-query. It will only returns rows that has a match between the two tables.
SELECT a.*
FROM tableA a
JOIN (SELECT query, status
FROM tableB
ORDER BY ID desc
LIMIT 1) b ON a.query = b.query AND a.status = b.status
use join and subquery
select a.* from
tablea a join
(
select * from table_name t1
where id =( select max(id) from table_name t2 where t1.query=t2.query)
) b join on a.query=b.query and a.status=b.status
select *
from A a
inner join B b on b.query = a.query
inner join
(select max(id) id, query
from B
group by query) gq on gq.id = b.id and gq.query = b.query
where a.status = b.status

Create a Join leaving one row for any possible duplicate

I have a 2 tables that looks like this
How can I call the same column wihout duplicating it
TYSM for help
Please try below mentioned Query:
select distinct t2.data,t1.key,t1.data from t1.table1 JOIN table as t2 ON t1.key = t2.key
You could assign a row number using a variable to t2 then join to t1 supressing the output of the t1.key.
for example
drop table if exists t1,t2;
create table t1 (id int);
create table t2 (id int, name varchar(2));
insert into t1 values(1),(2),(3),(4);
insert into t2 values(1,'s1'),(1,'s2'),(2,'s3'),(3,'s4'),(4,'s5');
select s.id, s.name,
case when s.rn = 1 then s.rn
else ''
end as something
from t1
join
(
select t2.id,t2.name,
if(t2.id <> #p, #rn:=1,#rn:=#rn+1) rn,
#p:=t2.id
from t2,(select #rn:=0,#p:=0) r
) s on t1.id = s.id
order by t1.id, s.name
Result
+------+------+-----------+
| id | name | something |
+------+------+-----------+
| 1 | s1 | 1 |
| 1 | s2 | |
| 2 | s3 | 1 |
| 3 | s4 | 1 |
| 4 | s5 | 1 |
+------+------+-----------+
5 rows in set (0.00 sec)

SQL select 2 tables but not ommit blank results

I have to do a select from 3 trables.
table1- idc,title,description..
table2- idc,filename,filepath,tabel1FK
table3- idc,table1FK,tabel2FK
I need a select from table1 and table2 and count unique ocurrences of table1 in table3
the select must be something like this
TB1 | TB2 | COUNT ON TB3
a | aa | 1
b | | 4
c | cc | 3
d | | 0
e | | 3
I think you just want left join and group by:
select tb1.idc, tb2.idc, count(tb3.idc)
from tb1 left join
tb2
on tb2.table1FK = tb1.idc left join
tb3
on tb3.table1FK = tb1.idc and tb3.tabel2FK = tb2.idc
group by tb1.idc, tb2.idc;

Combine two selects into one

I have a couple of tables. I'll try to make it simple: Table_1 ID is unique. Table_2 ID is not unique. The table_2 stores the ID from a row on table_1 and a value, resulting in, for instance, this:
table_1
ID | A
------
1 | a
2 | b
3 | c
table_2
ID | B
------
1 | x
1 | y
3 | z
I want to count how many of each ID is there on table_2, so I do
select t1.id, count(*)
from table_1 t1
group by t1.id
id | count
----------
1 | 2
3 | 1
And I want to list every row on table_2 and its corresponding value on table_1.A, so I do
select t1.id, t1.A, t2.B
from table_2 t2
left join table_1 t1
on t1.id = t2.id
ID | A | B
----------
1 | a | x
1 | a | y
3 | c | z
Is there a way to combine those 2 selections into one, to get a result like this?
ID | A | B | count
------------------
1 | a | x | 2
1 | a | y | 2
3 | c | z | 1
You can combine the results by joining the count result.
Fiddle with sample data
select t1.id, t1.A, t2.B, x.cnt as count
from t2
left join t1
on t1.id = t2.id
join (select t2.id, count(*) as cnt
from t2
group by t2.id
) x
on x.id = t1.id
Besides the generic Derived Table solution posted by #vkp you might also utilize a Scalar Subquery to return a single value:
select t1.id, t1.A, t2.B,
(select count(*)
from t2 as x
where x.id = t2.id
) cnt
from t2
http://www.sqlfiddle.com/#!9/07a3a/6

count value for each unique id

I have a table
id value
1 a
2 a
3 b
4 b
5 b
6 c
My id is primary.
I have total 2 a , 3 b and 1 c. So I want to count total repeat value in each primary id which matches on it
I want this format
id value_count
1 2
2 2
3 3
4 3
5 3
6 1
Try this query:
SELECT a.id, b.valueCnt
FROM tableA a
INNER JOIN (SELECT a.value, COUNT(a.value) valueCnt
FROM tableA a GROUP BY a.value) AS B ON a.value = b.value;
Check the SQL FIDDLE DEMO
OUTPUT
| ID | VALUECNT |
|----|----------|
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 1 |
Try This
select id, value_count from tablename as a1
join (select count(*) as value_count, value from tablename group by value) as a2
on a1.value= a2.value
I suggest you use a subselect without any joins:
SELECT
a.id,(SELECT COUNT(*) FROM tableA WHERE value = a.value) as valueCnt
FROM tableA a
Fiddle Demo
You need to use subquery.
SELECT table.id , x.value_count
FROM table
INNER JOIN
(SELECT t1.value, count(t1.id) as value_count
FROM table t1
Group by t1.value
) x on x.value = table.value