Mysql query question

Hi Experts!



I've got mysql query:
$query1c=mysql_query("SELECT Product, COUNT(Product) FROM data_queries GROUP BY Product ORDER BY COUNT(Product) DESC");
     while($row = mysql_fetch_array($query1c)){
     echo $row['COUNT(Product)']." - ".$row['Product']."<br/>";
}

Open in new window

It works and displays what I want, but I want to display product only if it exists in other table - 'data_products' table:
$query1=mysql_query("SELECT Product FROM data_products");

Open in new window



example:
Current output of first query is:
3319 - maritime digital services
520 - Maritime digital services II
33 - NK
8 - Paper charts and publications manager
5 - MDS
2 - Admiralty list of lights
1 - test
1 - Lloyds port guide
1 - Admiralty e-navigator

List of products (output of second query) is:
maritime digital services
Maritime digital services II
Admiralty list of lights

Expected output of first query (that's what I want):
3319 - maritime digital services
520 - Maritime digital services II
2 - Admiralty list of lights



Please help.
Thanks.
LVL 8
ZadoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StingRaYCommented:
$query1c=mysql_query("SELECT data_queries.Product product, COUNT(data_queries.Product) rowcount FROM data_queries INNER JOIN data_products ON data_queries.Product = data_products.Product GROUP BY Product ORDER BY COUNT(Product) DESC");
     while($row = mysql_fetch_array($query1c)){
     echo $row['rowcount']." - ".$row['product']."<br/>";
}

Open in new window

0
ZadoAuthor Commented:
Thanks for quick response StingRaY.
Hm, doesn't work for me, I checked all lowercases/uppercases and column names, all ok, something's wrong with your query I suppose.
0
Pratima PharandeCommented:
query1c=mysql_query("SELECT Product, COUNT(Product) FROM data_queries inner join data_products  on data_queries.Product =data_products.Product GROUP BY Product ORDER BY COUNT(Product) DESC");
     while($row = mysql_fetch_array($query1c)){
     echo $row['COUNT(Product)']." - ".$row['Product']."<br/>";
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZadoAuthor Commented:
Doesn't work either, I'll recheck my code, names etc., maybe it's me.
0
Amar BardoliwalaCommented:
Hello Zado,

another way would be to change query as following

query1c=mysql_query("SELECT Product, COUNT(Product) FROM data_queries where Product in(select distinct Product from data_products) GROUP BY Product ORDER BY COUNT(Product) DESC");

while($row = mysql_fetch_array($query1c)){
     echo $row['COUNT(Product)']." - ".$row['Product']."<br/>";
}

Hope this will help you.

Thank You.

Amar Bardoliwala.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZadoAuthor Commented:
Thanks Amar, works perfectly!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.