SQL Server 2008 query to get mixed data from two tables - sql-server-2008

Someone please tell me the SQL query to get the result....
Thanks!
From the above two tables, I want to get the Photo, Name, Id of max(id) from Table-A for each category_id for which the parent_id in Table-B is 1 i.e.
1005 E Byte Apple 3
1002 B Byte Banana 5
1007 G Byte Orange 6
1011 K Byte Mango 7

select a.id,a.name,a.photo,b.category_name,b.category_id
from table-A a join table-B s ON a.category_id = b.category_id
where parent_id = 1

Try the below;
Select TBLA.ID, TBLA.Name, TBLA.Photo, TBLB.Category_Name, TBLB.Category_ID
From [table-B] TBLB
Inner Join [table-a] TBLA On TBLA.Category_ID = TBLB.Category_ID
Where TBLB.Parent_ID = 1
And TBLA.ID = (Select Max(ID)
From [table-a]
Where Category_ID = TBLB.Category_ID)

Related

Select values in SQL that have a colum value in one row and not a specific other column value in another

From the following table, I would like to return the set of all books that have topic 3 but do not have topic 4.
id book topic
10 1000 3
11 1000 4
12 1001 2
13 1001 3
14 1002 4
15 1003 3
The correct table should be:
book
1001
1003
I made a SQL Fiddle with this for testing here.
So far, I tried the following, but it returns 1000, 1001, 1003 (because I am not comparing two rows with each other, and do not know how to do):
SELECT DISTINCT a.id
FROM TOPICS a, TOPICS b
WHERE a.id = b.id
AND a.topic = 3
AND NOT EXISTS (
SELECT DISTINCT c.id
FROM TOPICS c
WHERE a.id = c.id
AND b.topic = 4
)
You're almost there. The not exists condition is definitely the right idea, you just need to apply another one of these for checking the existence of topic 3:
SELECT DISTINCT a.book
FROM topics a
WHERE EXISTS (SELECT *
FROM topics b
WHERE a.book = b.book AND b.topic = 3) AND
NOT EXISTS (SELECT *
FROM topics b
WHERE a.book = b.book AND b.topic = 4)
Since you're using SQL Server (at least in the fiddle) you could use the exceptset operator:
SELECT DISTINCT book FROM TOPICS WHERE topic = 3
EXCEPT
SELECT DISTINCT book FROM TOPICS WHERE topic = 4
This would return 1001 and 1003.
Using NOT EXISTS:
SELECT a.book
FROM TOPICS a
WHERE a.topic = 3
AND NOT EXISTS
(SELECT 1
FROM TOPICS b
WHERE a.book = b.book
AND b.topic = 4)

MySQL multiple inner join not returning rows

I'm trying to create a query which selects from three tables.
m_release
---------------------------
release_id name
---------------------------
1 release1
2 release2
3 release3
mk_release_artist
---------------------------
release_id artist_id
---------------------------
1 134
2 135
mk_release_remix
---------------------------
release_id artist_id
---------------------------
3 134
I've created the following query so far, but it doesn't return any rows:
SELECT * FROM m_release A
JOIN mk_release_artist B ON A.release_id = B.release_id AND B.artist_id = 134
JOIN mk_release_remix C ON A.release_id = C.release_id AND C.artist_id = 134
It is working when i'm selecting from two tables using one JOIN
SELECT * FROM m_release A
JOIN mk_release_artist B ON A.release_id = B.release_id AND B.artist_id = 134
The output i'm expecting to see is:
---------------------------
release_id name
---------------------------
1 release1
3 release3
SELECT A.*
FROM m_release A
LEFT JOIN mk_release_artist B ON A.release_id = B.release_id
LEFT JOIN mk_release_remix C ON A.release_id = C.release_id
WHERE 134 in (B.artist_id, C.artist_id)
So, in B, author_id is the author, while in C, it's the remixer. Then your query selects entries where BOTH the author and the remixer are the id=134. Which has no matching entries in your example.
The juergen d's query would select entries where either author or the remixer is the id. Since it uses LEFT JOINs, it will even get the releases that have no corresponding entry in either B or C (the corresponding columns will be NULL).

Mysql Table Structure Working Fast?

I am planning to create a website similar to IMDB.com. To reduce execution time I am using the following structure. Is it okay for faster working?
Table - 1
Id Movie_name description
1 name one some description
2 name two some description
3 name three some description
Table 2
id actorname
1 name 1
2 name 2
3 name 3
4 name 4
Table 3
id movieid actorid
1 1 1
2 1 2
3 1 3
4 1 9
5 2 6
6 2 5
7 2 8
8 2 1
When I want to list actors in a movie program will retrieve actors ids from table 3 and find respective names from table 2 (using single query). When I want to list the movies of a actor it will retrieve movie ids from table 3 and find respective names from first table. Will it work properly? Any other ideas?
This will give all actors in a specified movie,
SELECT c.ID, c.actorName
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE a.ID = 1
This one will give all movies for a specified actor
SELECT a.*
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID = 1
SQLFiddle Demo (both queries)
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
This is called Relational Division
SELECT a.ID, a.Movie_Name
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID IN (1, 2, 3)
GROUP BY a.ID, a.Movie_Name
HAVING COUNT(DISTINCT c.ID) = 3
SQL of Relational Division
I suggest that you modify table3 by taking away the id field. Use the movieid and actorid together as your primary key. You might want to add other fields to this table such as name of character and order of appearance as suggested in the comment by Jermaine Xu.

Mysql joining two tables

I need to join the following two tables
table_A
id userId name score game
1 2343 me 45 Palo Alto
2 6575 other 21 SF
3 6575 other 2 miami
table_B
id userId pen mango
1 2343 3 4
2 2343 5 7
3 6575 1 2
Here is the join:
SELECT COUNT(a.userId), SUM(b.pen), SUM(b.mango)
FROM table_A AS a
LEFT JOIN table_B b ON a.userId = b.userId
WHERE userId = 2343;
The problem is I am getting count(userId) equals to 2, but I need it to be 1. What am I doing wrong?
Change it to the following:
count(distinct a.userId)
Your query will gerneate two rows, each row corresponding to one row in table_B (one for id 1, one for id 2).
I am not sure why you think the resulting count(a.userId) should be 1, but you could enforce this by using a GROUP BY clause à la GROUP BY b.userId.
I am confused about what you are doing. You can still get SUM of Pen and Mango without joining the two tables. And another thing, why do you still use the COUNT function where, in fact, you know that you are querying to ONLY ONE ID? Right?
SELECT SUM(Pen) as TotalPen,
SUM(Mango) as TotalMango
FROM table_B
WHERE userId = 2343
But if you want a joined tables you could write something like this:
SELECT SUM(COALESCE(b.pen,0)) as TotalPen,
SUM(COALESCE(b.mango,0)) as TotalMango
FROM table_A AS a LEFT JOIN table_B b ON a.userId = b.userId
WHERE a.userId = 2343;
The problem is I am getting count(userId) equals to 2, but I need it to be 1. - The query is correct but your understanding is wrong. Obviously there are two record IDs of 2343 in Table_B

MYSQL Subtables Grouping

If I have 2 tables:
A B
joe 1
joe 2
kevin 3
B C
1 1
1 2
1 3
2 2
2 3
3 3
What is the best way to get the subgroups when I search for column A?
i.e. for joe, i want to return 1:{1,2,3} and 2:{2,3}.
I know that I can iterate through multiple SELECT * FROM queries, but is there a way to do it in one query?
As a followup,
If I had a third table,
C D
1 x
2 y
3 z
How do I table 2 and table 3 together and then group by B?
I tried
select
tbla.id, tbla.name, group_concat(tblb.value)
from tbla
left join tblb
on tbla.id = tblb.a_id
group by tbla.id
left join tb1c
on tb1b.id=tb1c.id
and it does not seem to work
group_concat
E.g.
select
tbla.id, tbla.name, group_concat(tblb.value)
from tbla
left join tblb
on tbla.id = tblb.a_id
group by tbla.id ;
you might want to try this.
SELECT GROUP_CONCAT(TableB.C) as iResult
FROM tableB INNER JOIN tableA
on TableB.B = tableA.B
WHERE tableA.A = 'joe'