Results 1 to 6 of 6
Thread: joins in sql
- 07-03-2009, 01:07 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
joins in sql
can we do looping in select query.
suppose i have 3 tables
purchase table, item table, and purchase_item_relation table
in purchase table i just enter purchase date and location
and item table i enter all the items purchased
and in purchase_item_relation table i enter purchaseId, itemId
exaple
if i purchase Laptop,Mouse,Speakers on some date and at some location
so there will be only 1 record in purchase table and 3 records in items table and 3 records purchase_item_relation
purchase table:
Date............Location.........Purchase_id
24/12/2009....Dadar............123
Item table:
Item_name.............Item_id
Laptop...................21
Mouse....................22
Speaker..................23
relatio table:
Purchase_id........Item_id
123....................21
123....................22
123....................23
now i want to display this record like this
purchasedate......location........item
-------------------------------------
24-2-2009..........dadar...........laptop,mouse,spaeker s
if we do join we will get three records but i want only one record like above (all three items in oe colum).
please help i want this from select queryLast edited by katkamravi; 07-03-2009 at 01:47 PM.
- 07-08-2009, 02:56 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
CREATE TABLE purchase --table:
(Date DATETIME,
Location VARCHAR(100),
Purchase_id INT)
INSERT INTO purchase
SELECT '2009-12-24', 'Dadar', 123
CREATE TABLE Item --table:
(Item_name VARCHAR(100),
Item_id INT)
INSERT INTO Item
SELECT 'Laptop', 21 UNION ALL
SELECT 'Mouse', 22 UNION ALL
SELECT 'Speaker', 23
CREATE TABLE relatio --table:
(Purchase_id INT,
Item_id INT,)
INSERT INTO relatio
SELECT 123,21 UNION ALL
SELECT 123,22 UNION ALL
SELECT 123,23
-- Starting data
SELECT * FROM purchase
SELECT * FROM Item
SELECT * FROM relatio
SELECT
Date,
Location,
STUFF((SELECT ',' + Item_name
FROM Item i INNER JOIN relatio r ON r.Item_Id = i.Item_Id
WHERE r.Purchase_Id = p.Purchase_id
FOR XML PATH(''))
,1,1,'') as [Item]
FROM purchase p:) :) :)
- 07-09-2009, 05:18 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Completely related with the Database, not with java. ;) Actually you can have more comments on a database forum.
- 07-09-2009, 06:54 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
sub query... poor performance...
- 07-09-2009, 07:02 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
i think you can get that 3 result row and change it in the java code
- 07-23-2009, 11:08 AM #6
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks