473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Joins in MSSQL

There are two tables ‘book’ & ‘author’.
We need a query to retrieve all books written by an author.
Primary key of ‘book’ is bookid, and of author.. it is ‘author id’.
There is another table ‘bookdetails’ which contains the field bookid, authorid

Kindly help..

Thank You..
Sep 1 '05 #1
14 15347
There are two tables ‘book’ & ‘author’.
We need a query to retrieve all books written by an author.
Primary key of ‘book’ is bookid, and of author.. it is ‘author id’.
There is another table ‘bookdetails’ which contains the field bookid, authorid

Kindly help..

Thank You..
-------------------------------------------------------
Hello !
First consider table 'book' which has columns bookid, book_name and price.. out of which bookid is primary key.
Then consider table 'author' which has columns authorid and author_name out of which authorid is primary key.
Then consider table bookdetails which has columns bookid, authorid and publication..... i just assumed ok !
Now we want to retrieve all the books wriiten by an author say his authorid is 1 ..... then the query goes as follows.... you change the authorid accd to you !

query as follows :
Expand|Select|Wrap|Line Numbers
  1. select 
  2.         a.book_name, 
  3.         b.author_name, 
  4.         a.price 
  5. from 
  6.         book a, 
  7.         author b, 
  8.         bookdetails c
  9. where 
  10.         a.bookid = c.bookid and 
  11.         c.authorid = b.authorid and 
  12.         b.authorid = 1
  13.  
  14.  
----------------------------------------
This is the way joining is done ! :)
Dec 6 '05 #2
hi,

Expand|Select|Wrap|Line Numbers
  1. select book.name,author.name from book,auther where book.bookid=auther.authorid
or

Expand|Select|Wrap|Line Numbers
  1. SELECT book.name,author.name
  2. FROM  book
  3. INNER JOIN auther
  4. ON book.bookid=auther.authorid
  5.  
bye
ganesh.k
Aug 8 '06 #3
Dear,
Your code is
Expand|Select|Wrap|Line Numbers
  1.  mysql_query("Select table1.fieldname,..........,table2.fieldname from table1,table2 where your matching condition");
  2.  
Aug 10 '06 #4
zykes
14
if your using Enterprise Manager for the MS SQL, try using the view option. It's better you know...
Aug 18 '06 #5
Eek.
Richasarf's example was the best, though i prefer to use joins when writing in MS SQL. Thus:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   bok.book_name,
  3.   aut.author_name
  4. FROM
  5.   books AS bok
  6. JOIN bookdetails AS bde ON
  7.   bde.book_id = bok.id
  8. JOIN authors AS aut ON
  9.   bde.author_id = aut.id
  10. WHERE
  11.   author_name = 'authors name'
  12.  
The examples with a straight join are plain wrong. they:
a) don't take into account that you clearly have a many-to-many relationship (ie an author writes many books, but equally a book may have many authors) and
b) will give you back nonsense data. ganesh's example will give you the author for a book where their ids coincidentally match but are in fact totally unrelated.

As for the suggestion that you use the enerprise managers visual tool, i recommend against it as writing your own sql straight will mean you can transport your skills across platforms (of course each platform has their own idiosyncracies, the JOIN is classic sql server, richasaraf's example is classic PSQL)
Sep 12 '06 #6
I am a beginner, and I am learning about normalization and many to many relationships. Selecting all books written by an author is admittedly over my head, but manageable. I can take these examples, decode the SQL, and apply them just fine.

But what I cannot figure out is a more complicated search, where the input is one book, and the results are the author who wrote that book, PLUS the rest of the books that the author wrote.

I can't do it without performing another separate query. I'm hoping it could be some kind of subselect, alias, temporary table, or self join, but don't know how to do it. The rest of the books by an author are filtered out because they don't match the first book. Is there a way to do this without an additional query?
Oct 24 '06 #7
I can't do it without performing another separate query. I'm hoping it could be some kind of subselect, alias, temporary table, or self join, but don't know how to do it. The rest of the books by an author are filtered out because they don't match the first book. Is there a way to do this without an additional query?

Dont know if this helps you but this is 2 diffrent ways to do the same thing

We have a TABLE called tbl_books
with two columns
bookname & author

We want to see all books the author that wrote Lord of the Rings have published



Expand|Select|Wrap|Line Numbers
  1. SELECT     bookname
  2. FROM         tbl_books
  3. WHERE     (author =
  4.                           (SELECT     author
  5.                             FROM     tbl_books     AS     tbl_books2
  6.                             WHERE      (bookname = 'Lord of the ring')))
This is a then 2:nd way.

Expand|Select|Wrap|Line Numbers
  1. SELECT     tbl_books.bookname
  2. FROM         tbl_books INNER JOIN
  3.                       tbl_books AS tbl_books2 ON tbl_books.author = tbl_books2.author
  4. WHERE     (tbl_books2.bookname= 'Lord of the ring')
Hop it way this you wanted help with
Oct 25 '06 #8
Ah!

Thanks for the reply. It helps tremedously in clarifying how these more complicated searches work. I can comprehend the subselect syntax easily, but I'm going to need to study that inner join a bit more.
Oct 25 '06 #9
Or use a derived table. The derived table gives you your desired authorID and you join to it as if it was a normal table

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   boo.*
  3. FROM
  4.   books
  5. JOIN
  6. (
  7.   SELECT b.authorID
  8.   FROM books AS b
  9.   WHERE b.name = "lord of the rings"
  10. ) AS thisBook ON
  11.  thisBook.authorID = books.authorID
Jun 11 '08 #10
if you can cleary explain me, i will give yu an exact query
i want to know what are the columns in the table books,authors,bookdetails and whar are the primary key in that tables
Oct 4 '10 #11
NeoPa
32,556 Expert Mod 16PB
Have a look at SQL JOINs.

I'm afraid that much of what is posted in this thread is misleading and unhelpful. None of your tables should be linked without either a LEFT {OUTER} JOIN or an INNER JOIN. To use no joins at all would cause grievous and unnecessary overhead to the server.
Oct 5 '10 #12
what are you trying to say ?
Oct 6 '10 #13
ck9663
2,878 Expert 2GB
The post is more than four years old...

Relax everyone...

~~ CK
Oct 6 '10 #14
NeoPa
32,556 Expert Mod 16PB
Bharathk27:
what are you trying to say ?
Exactly what I have posted. In case you're unsure, nothing I said was directed at anything you'd posted (in fact I'm pleased you posted and drew my attention to this thread).

@CK.
The last post date of threads is pretty immaterial. If you check the number of visits this thread has achieved you'll see it's probably still quite busy as far as searchers go. I have no intention of being disrespectful to any of our members, but I would hate to feel people are still searching on Bytes and finding suggestions to join tables using a Cartesian Product then filtering out all the rubbish.
Oct 6 '10 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: badz | last post by:
Hi frenz Lately I try to use MSSQL and PHP , the problem arise when PHP try to read MSSQL field with 'image' data type, header("Content-type: image/jpeg"); // act as a jpg file to browser I...
7
by: mj | last post by:
Hello, thanks for the help. I am running a WinXP Pro w/ SP2 (my home computer, with ZoneAlarm firewall) Apache 2.0.52 MySQL 4.1.7 PHP 5.1.0-dev I have developed a PHP/MySQL web app that...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
2
by: raulgz | last post by:
Hi I work ith sql server 2000 and i need know the diferent of joins in format not ansi ( with * ) and joins in format ansi ( with 'outher join on' ). Two format work equal ???
3
by: ree | last post by:
I got this problem where I need to look up values in two columns from another table. I can get OUTER LEFT JOIN working when looking up one column from a table but when looking up at two columns...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
14
by: Kukurydz | last post by:
I've got such problem: My database is stored on MSSQL Server. I have to write reports for it in MSAccess. I've got a problem with creating a query which will select records from MSSQL table with...
0
by: Derftics | last post by:
Hi Guys, Is there anyone who have tried installing MSSQL 2000 and MSSQL 2005 servers in one desktop computer? I have tried using MSDE and successfully install the MSSQL 2000 server but when I...
1
by: mkepick | last post by:
migrated databases from sybase to mssql, migration exported sybase written queries written with the application build in query tool and imported to mssql databases. all db and tables migrated...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.