MySQL query problem

pstylesss

Platinum Member
Mar 21, 2007
2,914
0
0
I need a little help pulling some info from a MySQL DB. I have 3 tables, accounts, subscriptionItems, & subscriptionList

accounts contain user information and accountID

These are the tables in my DB

subscriptionItems
subscriptionID
name
status

subscriptionList
subscriptionListID
accountID
subscriptionID



subscriptionItems hold the names of the things people can subscribe to. subscriptionList holds the actual list of subscribers. What I want to do is list all the names of items, then to the right of them, list the amount of subscribers. What I have kindof works, but not correctly. Below is what I am using.

<table width="450" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="210" align="center"><strong>Subscription Name </strong></td>
<td width="175" align="center"><strong>Options</strong></td>
<td width="65" align="center"><strong>Amount</strong></td>
</tr>
<?
$result = mysql_query("SELECT * FROM subscriptionItems ORDER BY name ASC",$connect);
while($row = mysql_fetch_assoc($result)){
$subscriptionID = $row['subscriptionID'];
$name = $row['name'];
$color1="#FFFFFF";
$color2="#EEEEEE";
$bgcolor=($bgcolor==$color1)?$color2:$color1;
?>

<tr bgcolor="<?=$bgcolor;?>">
<td><?=$name?></td>
<td align="center">Edit - Delete </td>
<td align="center"><?
if(isset($subscriptionID)){
$result2 = mysql_query("SELECT * FROM subscriptionList WHERE subscriptionID='$subscriptionID' ",$connect);
while($row2 = mysql_fetch_assoc($result2))
$amount = mysql_num_rows($result2);
echo $amount;
}else{
echo $amount;
}
?></td>
</tr>
<?
}
?>
</table>

Here is the output:
------Subscriptions---------------------------------Options-------------------Amount
Construction/Road Closures------------------ Edit - Delete-----------------------1
Pool Updates-------------------------------------Edit - Delete-----------------------2
Recreation Updates-----------------------------Edit - Delete-----------------------2
Senior Center------------------------------------Edit - Delete-----------------------2

in Pool Updates, Recreation Updates, and Senior Center there are 0 subscribers, so the result should be zero. I'm sure what is happening is when it is just sticking with the last $amount variable given... but I'm not sure how to fix it.

I tried joins, but they didn't seem to be working, at at this point, they seem a tad over my head.

Thanks for any help!
 

aceO07

Diamond Member
Nov 6, 2000
4,491
0
76
I didn't look through your entire post, but I think this is what you're looking for. I think...

Select subscriptionID, count(subscriptionID) as amount from subscriptionList group by subscriptionID
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
ace007 gave you a clue, however you want to join it.

SELECT subscriptionItems.name, COUNT(subscriptionList.SubscriptionId)
FROM subscriptionItems
LEFT JOIN subscriptionList ON subscriptionItems.subscriptionId = subscriptionList.subscriptionId
GROUP BY subscriptionItems.name

(I"m not really famililar with the MySQL syntax, and I ddin't test this in any sql tool and of the top of my head, so it could have syntax errors)
 

pstylesss

Platinum Member
Mar 21, 2007
2,914
0
0
Thats great guys, thanks! I'll take a look at this in a minute and see how it works...
 

pstylesss

Platinum Member
Mar 21, 2007
2,914
0
0
Originally posted by: aceO07
I didn't look through your entire post, but I think this is what you're looking for. I think...

Select subscriptionID, count(subscriptionID) as amount from subscriptionList group by subscriptionID

Almost had it with this one. Returned half of what I needed.

Originally posted by: brandonb
ace007 gave you a clue, however you want to join it.

SELECT subscriptionItems.name, COUNT(subscriptionList.SubscriptionId)
FROM subscriptionItems
LEFT JOIN subscriptionList ON subscriptionItems.subscriptionId = subscriptionList.subscriptionId
GROUP BY subscriptionItems.name

(I"m not really famililar with the MySQL syntax, and I ddin't test this in any sql tool and of the top of my head, so it could have syntax errors)

This worked PERFECTLY first try. Thanks a bundle, saved me a huge headache. I suppose I should learn MySQL syntax a bit more.
 
Dec 29, 2005
89
0
0
brandonb's solution should be correct (in theory at least, i can't verify mysql syntax right now).

and also to the OP, i don't want to sound insulting or anything here, but joins are a very low level/simple aspect of SQL. meaning that if you are using sql you should be familiar with how they work. i'm mentioning this because you may have other areas in your application which perform similar tasks, and it would probably be much more efficient to change the code to use joins (why is it more efficient?. not making multiple connections/requests to the database - like in a while loop, letting the dbms handle the query - dbms developers spend alot of time optimizing things like joins to put the least amount of stress on the dbms so you might as well use the product of their labor).

once again, my intention here is not be negative, but to help. learning about how joins work will help you, save you time, and allow you to create a working solution.
 

pstylesss

Platinum Member
Mar 21, 2007
2,914
0
0
I could probably figure this out if I tried long enough, but since both of you are still online, I figure I might ask.

COUNT() worked just fine in when I ran the query in phpmyadmin, but I'm not figuring out how to get it into a php variable. How would I do that?
 

pstylesss

Platinum Member
Mar 21, 2007
2,914
0
0
Originally posted by: shader
brandonb's solution should be correct (in theory at least, i can't verify mysql syntax right now).

and also to the OP, i don't want to sound insulting or anything here, but joins are a very low level/simple aspect of SQL. meaning that if you are using sql you should be familiar with how they work. i'm mentioning this because you may have other areas in your application which perform similar tasks, and it would probably be much more efficient to change the code to use joins (why is it more efficient?. not making multiple connections/requests to the database - like in a while loop, letting the dbms handle the query - dbms developers spend alot of time optimizing things like joins to put the least amount of stress on the dbms so you might as well use the product of their labor).

once again, my intention here is not be negative, but to help. learning about how joins work will help you, save you time, and allow you to create a working solution.

I'm not insulted in the least. I asked for help because I have hardly used them and I wanted to learn, and actually needed to learn. This DB will eventually get huge, and I knew joins took away a lot of stress, but up until now there wasn't a huge need for them in anything I've coded. I need and want to expand my sql knowledge.
 
Dec 29, 2005
89
0
0
you probably need to assign it a name in the sql syntax.

an example:

i have a table T with two columns, a and b. however when i want to select the b column i want it to be called c (this is just an example, dont ask me why you would want to do this)

here is how:

select a, b as c from T;

it might also work with:

select a, b c from T;

some sql vendors allow you to omit the as keyword. b as c is the part that is crucial.

so for your problem:

SELECT subscriptionItems.name, COUNT(subscriptionList.SubscriptionId) as cnt
FROM subscriptionItems
LEFT JOIN subscriptionList ON subscriptionItems.subscriptionId = subscriptionList.subscriptionId
GROUP BY subscriptionItems.name

and then when you pull the field from you will want to use the row name of 'cnt' to get those results

i think it will be:

$count = $row['cnt'];
 

pstylesss

Platinum Member
Mar 21, 2007
2,914
0
0
wonderful shader. I should have figured that out by looking at aceO07's post. As soon as I saw "as" in your post, I remembered seeing it in Ace's.

Thanks everyone, great help!
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Originally posted by: brandonb
ace007 gave you a clue, however you want to join it.

SELECT subscriptionItems.name, COUNT(subscriptionList.SubscriptionId)
FROM subscriptionItems
LEFT JOIN subscriptionList ON subscriptionItems.subscriptionId = subscriptionList.subscriptionId
GROUP BY subscriptionItems.name

(I"m not really famililar with the MySQL syntax, and I ddin't test this in any sql tool and of the top of my head, so it could have syntax errors)

This is fine as long as subscriptionItems.name is guaranteed to be unique. If not, you could do this:

SELECT subscriptionItems.id, subscriptionItems.name, COUNT(subscriptionList.SubscriptionId)
FROM subscriptionItems
LEFT JOIN subscriptionList ON subscriptionItems.subscriptionId = subscriptionList.subscriptionId
GROUP BY subscriptionItems.id, subscriptionItems.name

Or split it into two queries - aceO07's to get the ID and count, and another to fetch the name with the ID.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |