extracting row values from a single table with a condition - sql-server-2008

I do have a table contains 3 columns like
ID Name RID
1 xx 4
2 yy 3
3 zz 2
4 aa 1
Now I want the result as
ID Name Rname
1 xx aa
based on the RID it will refer to the ID column and brings the value of Name column as Rname. Please help me the query.

This will return the desired result:
select t1.ID, t1.Name, t2.Name as Rname
from Table t1
join Table t2 on t1.RID = t2.ID
where t1.ID = 1

You can do this with a self join:
SELECT a.id, a.name AS name, b.name AS rname
FROM mytable a
JOIN mytable b ON a.rid = b.id AND a.rid > b.rid

Related

FETCH not associated field_name from mysql table

i have tables like below,
Table 1 :
ID
FIELD_NAME
1
field_1
2
field_2
3
field_3
Table 2 :
ID
TAG_NAME
1
tag_1
2
tag_2
3
tag_3
4
tag_4
Table 3 :
FIELD_ID
TAG_ID
1
1
1
2
1
3
2
1
3
2
3
3
so table-3 shows :
field1(from table1) connected with all 3 tag id's of table-2
field2(from table1) connected with all tag id=1 of table-2
and
field3(from table1) connected with all tag id=2,3 of table-2
I like to write a MYSQL Query to fetch
for a given tag_name, find all field name which are NOT associated with this
Example :
input = tag_1
output = field_3
Explanation : as field_3 not have any relation with tag_1 id
input = tag_4
output = field_1, field_2, field_3
Explanation : as no field have any relation with tag_4 id
You could check for fields where certain link table entries do not exist...
SELECT
*
FROM
table1
WHERE
NOT EXISTS (
SELECT *
FROM table3
WHERE field_id = table1.id
AND tag_id = (SELECT id FROM table2 WHERE tag_name = 'tag_4')
)
(Please excuse typos, I'm on my phone.)
dbfiddle as borrowed from #nbk : here
You can do it with a CROSS join of table1 to table2 and a LEFT join yo table3 where you return the unmatched rows:
SELECT t2.TAG_NAME, t1.FIELD_NAME
FROM table1 t1 CROSS JOIN table2 t2
LEFT JOIN table3 t3 ON t3.FIELD_ID = t1.ID AND t3.TAG_ID = t2.ID
WHERE t2.TAG_NAME = ? AND t3.FIELD_ID IS NULL
If you want the results as comma separated list you can also group by TAG_NAME and use GROUP_CONCAT():
SELECT t2.TAG_NAME, GROUP_CONCAT(t1.FIELD_NAME) fields
FROM table1 t1 CROSS JOIN table2 t2
LEFT JOIN table3 t3 ON t3.FIELD_ID = t1.ID AND t3.TAG_ID = t2.ID
WHERE t2.TAG_NAME = ? AND t3.FIELD_ID IS NULL
GROUP BY t2.TAG_NAME
Replace ? with the tag that you search for.
See the demo.

SQL JOIN and count without counting an ID twice

I am trying to join a table and get a count but I cannot count an ID twice in the table for the count.
Table 1:
ID animal
-- ------
1 dog
2 dog
3 cat
4 cat
5 dog
Table 2:
ID
--
2
2
3
5
5
I need to get a count of how many of each type of animal are in table 2. I can get it to join and change the ID to the type of animal and then get a count of each.
The issue is that each ID can only get counted once. So the expected output would be.
dog:2
cat:1
Where my output is
dog:4
cat:1
Try like below
select t1.animal, count( distinct t2.ID)
from table1 t1 join table2 t2 on t1.ID=t2.ID
group by t1.animal
You can try below using count distinct id
select b.animal,count(distinct a.id) from table2 a
inner join table1 b on a.id=b.id
group by b.animal
Try this:
SELECT t1.animal AS "Animal", COUNT(DISTINCT t1.ID) AS "No. of Animals"
FROM TABLE2 t2, TABLE1 t1
WHERE t2.ID = t1.ID
GROUP BY t1.animal
You can Try Nested Selects here.
SELECT
t.animal,
COUNT(t.ID) AS Count
FROM
(SELECT DISTINCT a.animal, b.ID FROM table1 a INNER JOIN table2 b ON a.ID = b.ID)t
GROUP BY t.animal
this is tested image

How to get a row with left join with two right table row matching two separate value in MySQL?

I want to get Ronald from Left Table through matching Right Table values with 1 AND 2. I know I need to use DISTINCT to get only one row but other than that, I'm stumped.
Left Table
pid | name
1 Ronald
2 Chris
3 John
Right Table
pid | value
1 1
1 2
2 1
3 2
Joined Table
pid | name | value
1 Ronald 1
1 Ronald 2
2 Chris 1
3 John 2
Expected Output
pid | name
1 Ronald
You want to match both 1 and 2. Aggregation and having come to mind:
select t1.pid, t1.name
from table1 t1 join
table2 t2
on t1.pid = t2.pid
where t2.value in (1, 2)
group by t1.pid, t1.name
having count(*) = 2; -- should be `count(distinct)` if duplicates are possible in `table2`
Self join table2 and select value = 1 in the first and value = 2 in the second. Join the result with table 1.
select t1.pid, t1.name
from table1 t1
join table2 t2_1 on t1.pid = t2_1.pid
join table2 t2_2 on t2_1.pid = t2_2.pid AND t2_1.value = 1 AND t2_2.value = 2
Or if you like that better:
select t1.pid, t1.name
from table1 t1
join table2 t2_1 on t1.pid = t2_1.pid AND t2_1.value = 1
join table2 t2_2 on t2_1.pid = t2_2.pid AND t2_2.value = 2

Same id, different name, how to get name by another name?

This is my table:
id name
1 j
1 jack
1 john
So I have j and want find all names which id is equal to the id of j in single SQL?
select name
from your_table
where id in
(
select id from your_table where name = 'j'
)
Self join time
select t1.name
from your_table t1
inner join your_table t2 on t2.id = t1.id
where t2.name = 'j'

querying 2 tables

good day, i have a problem in querying in 2 mysql table.
table 1
c_id c_name
**-----------------**
1 blah,blah
2 hey
table 2
m_id c_id m_name
-----------------------
1 1 some_name
2 1 some-name
3 1 some/name
4 1 some.name
5 2 name.some
6 2 name-some
i wanted the display to be like this:
c_id c_name m_name
-----------------------------
1 blah,blah some_name
some-name
some/name
some.name
2 hey name.some
name-some
Select a.c_id, a.c_name,
group_concat(b.m_name SEPARATOR '<br/>') as m_name from table1 a
left join table2 b on a.c_id = b.c_id
group by a.c_id;
Fiddle
Use m_name bind to a label. It will come with line breaks
This query will output exactly what you want :
select case when first.m_id is null then ' ' else t1.c_id end c_id,
case when first.m_id is null then ' ' else t1.c_name end c_name,
m_name
from Table1 t1
inner join Table2 t2 on t1.c_id = t2.c_id
left outer join (
select m_id,t1.c_id, t1.c_name, min(m_name)
from Table1 t1
inner join Table2 t2 on t1.c_id = t2.c_id
group by t1.c_id, t1.c_name) first on t2.m_id = first.m_id
order by t1.c_id, t2.m_id
See SQLFIDDLE : http://www.sqlfiddle.com/#!2/4bab3/16/0
You can use join to get the results of both tables together:
select c_id, c_name, m_name from table1 as x left outer join table2 as y on x.c_id = y.c_id;
Select a.c_id, a.c_name, b.m_name from table1 a left join table2 b on a.c_id = b.c_id;