All,
Say I have the following Select statement in mySQL:
$qry = "Select filename from upload where file_id='$file_id'";
$result = mysql_query($qry);
$resultset = mysql_fetch_array($result);
That query works fine but what I would also like to do is include a count of how many times that same filename appears somewhere else in that table in the same statement so I only have to run the mySQL statement once instead of getting that filename from this result and then executing another query like this one:
$qrycheck = "Select COUNT(filename) from upload where filename='$resultset[filename]'";
Is there anyway to do this check in a single mySQL statement?
Thanks!
If I understand correctly, you have several instances of ‘filename’ with different file_ids (I suppose that file_id is a unique ID, so it wouldn’t make sense to count occurrences of file_id – you’d either get 1 or 0):
If that’s the case, you have to JOIN upload with itself:
This will return the name in $resultset[‘filename’] and the number of instances in $resultset[‘instances’].