473,387 Members | 1,724 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,387 software developers and data experts.

SQL statment for muliplie tables including the count function .

Warning I'm new to anything other than basic SQL so this 'might' seem like a
daft question.
My problem is - I've have two tables one hold's photo details and is called
'PhotoTable' it is linked via it's key field 'PhotoID' to another table
'CommentsTable' that can hold multiple comments for each photo in the photo
table. For each comment in the comment table there is a flag field
'CommentVerified' that is changed to a '2' once that particular comment has
been assessed/verified.
What I would like is a SELECT statement that will pull all fields from the
photo table along with a count of the number of verified comments for that
particular photo (the actual photo records pulled should come from arguments
based on the photo table). So far the best I have is this -

SELECT * FROM PhotoTable , CommentsTable
WHERE PhotoTable.PhotoID = CommentsTable.PhotoID

But this has problems, The WHERE statement I want would be more like -
WHERE PhotoTable.PhotoCat = 'VariableValue' the one I have only pulls
records of photos that have at least one comment (I understand why, just not
how to change it)

Also instead of it just supplying the values of the comments verified fields
I would like a count of the number of relevant comments for each photo (only
those which are containing a '2')

OK I hope this makes some sense to someone out there.

Cheers
DyslexicFingars
Nov 12 '05 #1
2 1861
Try SELECT PhotoTable.*, CommentsTable.*,
FROM PhotoTable INNER JOIN CommentsTable ON PhotoTable.PhotoID =
CommentsTable.PhotoID
WHERE CommentsTable.CommentVerified = 2;

This will only select photos where there are comments, and in addition the
CommentVerified = 2

The JOIN bit defines how the tables relate to each other, the WHERE bit
applies constrains on which of the "combined" records are shown

Phil

"Dyslexic Fingars" <co*@yourattitudedyslexicfingars.net> wrote in message
news:xp******************@news-text.cableinet.net...
Warning I'm new to anything other than basic SQL so this 'might' seem like a daft question.
My problem is - I've have two tables one hold's photo details and is called 'PhotoTable' it is linked via it's key field 'PhotoID' to another table
'CommentsTable' that can hold multiple comments for each photo in the photo table. For each comment in the comment table there is a flag field
'CommentVerified' that is changed to a '2' once that particular comment has been assessed/verified.
What I would like is a SELECT statement that will pull all fields from the
photo table along with a count of the number of verified comments for that
particular photo (the actual photo records pulled should come from arguments based on the photo table). So far the best I have is this -

SELECT * FROM PhotoTable , CommentsTable
WHERE PhotoTable.PhotoID = CommentsTable.PhotoID

But this has problems, The WHERE statement I want would be more like -
WHERE PhotoTable.PhotoCat = 'VariableValue' the one I have only pulls
records of photos that have at least one comment (I understand why, just not how to change it)

Also instead of it just supplying the values of the comments verified fields I would like a count of the number of relevant comments for each photo (only those which are containing a '2')

OK I hope this makes some sense to someone out there.

Cheers
DyslexicFingars

Nov 12 '05 #2
Is this what you mean ?

SELECT * FROM PhotoTable , (SELECT Count(*) FROM CommentsTable
WHERE CommentsTable.PhotoID=PhotoTable.PhotoID
AND CommentsTable.CommentVerified=2) AS CountOfComments;

No join is used, so it will return 0 in the CountOfComments field where
appropriate.

HTH,
Ian.

"Dyslexic Fingars" <co*@yourattitudedyslexicfingars.net> wrote in message
news:xp******************@news-text.cableinet.net...
Warning I'm new to anything other than basic SQL so this 'might' seem like a daft question.
My problem is - I've have two tables one hold's photo details and is called 'PhotoTable' it is linked via it's key field 'PhotoID' to another table
'CommentsTable' that can hold multiple comments for each photo in the photo table. For each comment in the comment table there is a flag field
'CommentVerified' that is changed to a '2' once that particular comment has been assessed/verified.
What I would like is a SELECT statement that will pull all fields from the
photo table along with a count of the number of verified comments for that
particular photo (the actual photo records pulled should come from arguments based on the photo table). So far the best I have is this -

SELECT * FROM PhotoTable , CommentsTable
WHERE PhotoTable.PhotoID = CommentsTable.PhotoID

But this has problems, The WHERE statement I want would be more like -
WHERE PhotoTable.PhotoCat = 'VariableValue' the one I have only pulls
records of photos that have at least one comment (I understand why, just not how to change it)

Also instead of it just supplying the values of the comments verified fields I would like a count of the number of relevant comments for each photo (only those which are containing a '2')

OK I hope this makes some sense to someone out there.

Cheers
DyslexicFingars

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sparrow | last post by:
I have created a table with the following columns... Date(datetime),Actual (Int),Planned (Int) I need to insert weekending dates starting from 23/04/04 looping thru'for the next 52weeks...
8
by: 73blazer | last post by:
Hello, I'm looking for a way to make some of my insert templates more readable by placing comments in between the values. I cannot seem to find a way to do this with DB2, is there a way? I'm...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
1
by: Brian | last post by:
I'm trying to find a way to search multiple tables for the same record. Say I have 3 tables all with a name column, I need to search all 3 tables and find matching names. Is there an easy way to...
18
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. ...
8
by: Belee | last post by:
I have the following code and it is not passing through the Next statement: Private Function IsItemAlreadyAdded() As Boolean Dim drMyRow As DataRow With Me For Each drMyRow In...
5
by: Raphael Bauduin | last post by:
Hi, I'm looking at the logging of a database we'll put in production soon. I've seen some posts on this list about history tables, like mentioned in...
10
by: =?Utf-8?B?TWljaGFlbCBkZSBWZXJh?= | last post by:
To all, I'm using the current code below and for some reason I'm not getting anything into my variable. I'm expecting to see "efghijk". I have rebuilt my project/solution but still to no...
12
by: ArunDhaJ | last post by:
Hi Friends, Is it possible to pass a table as a parameter to a funtion. whos function declaration would look some thing like this.... ALTER FUNCTION TempFunction (@TempTable TABLE, @nPId INT) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.