473,471 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Query SQL Select with 2 table

Kosal
68 New Member
Could you help to give me the sample about SQL Query please?

I have 2 table :

- Table Product{pID, pName, pType, pDetail, pStock}
- Table ProductImage{imageID, pID, imageName, imageFile}

One Product have many Image so I want to query all product to display but for one product one image but I cannot query becuase when I query all product they show all product all image. but I need one product one image.
Feb 19 '10 #1
4 2295
OraMaster
135 New Member
Hi,

I guess you need below query to execute

select Product .*,ProductImage.*
from Product join ProductImage
on Product.pID = ProductImage.pID
But if above query is not useful to you then try to post the sample data for both tables along with expected output.
Feb 19 '10 #2
Kosal
68 New Member
OK sir

thanks you for your help.
Apr 3 '10 #3
nbiswas
149 New Member
I am presenting a scenario.

Suppose you have two tables tblProduct and tblProductImage

tblProduct
Expand|Select|Wrap|Line Numbers
  1. pId     pName
  2. 1    p1
  3. 2    p2
  4. 3    p3
  5. 4    p4
tblProductImage
imageId Pid PName

Expand|Select|Wrap|Line Numbers
  1. 1    1    image1_Prod1
  2. 2    1    image2_Prod1
  3. 3    1    image3_Prod1
  4. 4    2    image1_Prod2
  5. 5    2    image2_Prod2
  6. 6    3    image1_Prod3
  7. 7    3    image2_Prod3
  8. 8    4    image1_Prod4
As per your statement, the output should be

Expand|Select|Wrap|Line Numbers
  1. pid      pName      ImageId          ImageName
  2.  
  3. 1    p1    1    image1_Prod1
  4. 1    p1    2    image1_Prod1
  5. 1    p1    3    image1_Prod1
  6. 2    p2    4    image1_Prod2
  7. 2    p2    5    image1_Prod2
  8. 3    p3    6    image1_Prod3
  9. 3    p3    7    image1_Prod3
  10. 4    p4    8    image1_Prod4
i.e. for all products , all information pertaining to that product present in the productIamge table should appear but only the first image will come for every distinct product.

i.e. If product1 has 4 images(img1,img2,img3,img4), product2 has 3 images(img5,img6,img7) in the output all the informations will appear but for product1 always the image column will be filled with img1 and for product2 always will be img5 as per the example I have set.

If this asumption of mine is correct, then the query is

Expand|Select|Wrap|Line Numbers
  1. select 
  2.     x.pId
  3.     ,x.pName
  4.     ,x.imageID 
  5.     ,case when x.pId = y.pId then y.imageName end as imageName
  6.  
  7. from (    select 
  8.             p.pId
  9.             ,p.pName
  10.             ,i.imageName
  11.             ,i.imageID
  12.         from tblProduct p join tblImage i
  13.         on p.pId = i.pId)x
  14. join (    select 
  15.             imageID
  16.             ,pId
  17.             ,imageName
  18.          from(select dense_rank() over(partition by pid order by imageid) rn, imageID,pId,imageName from tblImage)x
  19.         where x.rn = 1) y
  20. on x.pId = y.pId
This code will work for Sql Server 2005+.

In your case just add the relevant column names for both the tables.

Hope this helps.
Apr 9 '10 #4
Kosal
68 New Member
Hello

Many thanks for your help.

Best regards
Kosal
Oct 19 '11 #5

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

Similar topics

0
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...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Ed_No_Spam_Please_Weber | last post by:
Hello All & Thanks in advance for your help! Background: 1) tblT_Documents is the primary parent transaction table that has 10 fields and about 250,000 rows 2) There are 9 child tables with...
4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
6
by: Larry R Harrison Jr | last post by:
I have a database I'm designing in Access 97. I have a custom field in a query which looks in {Table of Documents} and shows them all. It then needs a "latest revision number," stored in another...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
0
by: phlype.johnson | last post by:
I'm struggling to find the best query from performance point of view and readability for a normalized DB design. To illustrate better my question on whether normalized designs lead to more complex...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
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,...
1
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
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,...
1
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.