473,480 Members | 4,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to join to get unique values from tables, if table have one to many relation.

2 New Member
CREATE TABLE books
(
book_id bigint,
book_title varchar(100),
CONSTRAINT books_book_id_pkey PRIMARY KEY (book_id)
)WITH OIDS;

CREATE TABLE authors
(
id bigint,
book_id bigint,
author_name varchar(100),
CONSTRAINT authors_id_pkey PRIMARY KEY (id),
CONSTRAINT authors_book_id_fk FOREIGN KEY (book_id)
REFERENCES books (book_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)WITH OIDS;

INSERT INTO books (book_id,book_title) VALUES('1','PHP 5.X');
INSERT INTO books (book_id,book_title) VALUES('2','POSTGRESQL');

INSERT INTO authors (id,book_id,book_title) VALUES('1','1','Mangal Kumar');
INSERT INTO authors (id,book_id,book_title) VALUES('2','1','Pankaj Kumar');
INSERT INTO authors (id,book_id,book_title) VALUES('3','2','Brijesh Kumar');
INSERT INTO authors (id,book_id,book_title) VALUES('4','2','Mangal Kumar');

We want to select the books with their author names. We used following query which creates duplicate rows:

SELECT book_title, author_name from books JOIN authors ON (books.book_id = authors.book_id);

OUTPUT:

book_title author_name
-------------- -------------------
PHP5.X Mangal Kumar
PHP5.X Pankaj Kumar
POSTGRESQL Brijesh Kumar
POSTGRESQL Mangal Kumar

How to get the unique rows with book title and author name?
Apr 9 '10 #1
2 5279
rski
700 Recognized Expert Contributor
But the rows you put are unique?
I don't get what is your problem.
To get unique rows you can use DISTINCT, but in your example you don't need it.
How do you want the output of the query look like?
Apr 9 '10 #2
mangalv
2 New Member
CHECK THIS QUERY AND ITS OUTPUT:

SELECT DISTINCT books.book_id, book_title, author_name from books JOIN authors ON (books.book_id = authors.book_id) ORDER BY author_name:

OUTPUT:

book_id book_title author_name
-------------- -------------- ---------------------------
2 PHP5.X Brijesh Kumar
1 PHP5.X Mangal Kumar
2 POSTGRESQL Mangal Kumar
1 POSTGRESQL Pankaj Kumar

In the above output book_id is repeating, we want that query return unique ids only:
Apr 10 '10 #3

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

Similar topics

0
3044
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
1
3426
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
4
8082
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
2
2225
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
4
7635
by: Jim in Arizona | last post by:
This doesn't make any sense to me. I'm hoping some SQL guru out there knows the answer. I'm making a help desk type database system. There's only two tables, one called TTickets and the other...
33
2763
by: Steve | last post by:
One of our clients recently upgraded their Office version to 2003. When they tried to run our program (written in Access 2000), they ended up with the wrong data. My coworker and I have tested this...
7
4145
by: Shanimal | last post by:
I would like to know how to join 2 queries so that the results of these 2 queries show up in the same query: SELECT b.bios_serial_number FROM bios b: SELECT s.system_name FROM system s; ...
2
1313
by: JP2006 | last post by:
I have a situation where I have 2 tables - NODES and TAGS. Nodes is the main table and contains various columns one of which is tagID. Tags contains a list of tag names and tagIDs. When a user adds...
8
783
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation
6
3784
by: BD | last post by:
Hi, all. I need to enforce a one-to-many relationship on 2 tables, with a join table. Say the join table contains account information. It has cust_no and acct_no. Both cust_no and acct_no are...
0
7039
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,...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
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
5326
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,...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.