0

i have 2 tables as follow

sales (table1)
sales_id, userid(buyerid), product id, productname, orderid, status

users (table2)
userid, username, pass

Now, I need to display the top customer who makes regular purchase on my shopping site.

How can I achieve this?

I tried as

SELECT A.(*), B.username FROM sales A, users B WHERE A.userid=B.userid AND A.status='delivered';

Now, the same has to be looped and shown as top 10 buyers with the username, need to know how to loop too

Need experts advice on this, thanks

3
Contributors
11
Replies
18
Views
4 Years
Discussion Span
Last Post by sammry
0

Have you tried using an inner join, and ordering by the number of sales descending?

SELECT `u`.*, COUNT(`s`.*) AS `total_sales`
FROM `users` `u`
INNER JOIN `sales` `s` ON (`s`.`userid` = `u`.`userid` AND `s`.`status` = 'delivered')
ORDER BY `total_sales` DESC
LIMIT 10
0

Hi, cant i just select userid and username from the users table as the users table is too long and has 45 columns, so SELECT u.userid, u.username, COUNT(s.*).... should not do...

Meantime, will try your query and update you too... thanks

Edited by sammry: additional info

0

hi and also, I need to display where minimum of 10 or 15 purchases has to be made

0
SELECT `u`.`userid`, `u`.`username`, COUNT(`s`.*) AS `total_sales`
FROM `users` `u`
INNER JOIN `sales` `s` ON (`s`.`userid` = `u`.`userid` AND `s`.`status` = 'delivered')
HAVING COUNT(`s`.`id`) >= 10
ORDER BY `total_sales` DESC
LIMIT 10
0

Hi, its not showing any result, how shoulk i loop this with total_sales

0

its not showing any result

what do u mean
1) no rows showed?
2) query giving any error?

you may also try withoug 10 or 15 conition as given below

SELECT `u`.`userid`, `u`.`username`, COUNT(`s`.*) AS `total_sales`
FROM `users` `u`
INNER JOIN `sales` `s` ON (`s`.`userid` = `u`.`userid` AND `s`.`status` = 'delivered')
ORDER BY `total_sales` DESC
LIMIT 10

Edited by urtrivedi

0

Yeah I had tried with out 10 or 15 earlier but no luck... may be am not querying the loop exactly. can you help me with the complete query with displaying the row results. Sorry for the trouble, so much confused as of now. Thanks for your help and appreciate your help.

0

Can you run the query in PHP MyAdmin, or the MySQL console to confirm it's getting some results?

Then:

<?php

// Prepare and execute query
$sql = 'SELECT `u`.`userid`, `u`.`username`, COUNT(`s`.*) AS `total_sales`
FROM `users` `u`
INNER JOIN `sales` `s` ON (`s`.`userid` = `u`.`userid` AND `s`.`status` = 'delivered')
ORDER BY `total_sales` DESC
LIMIT 10';
$result = mysql_query($sql);

// Check if no results found
if(mysql_num_rows($result) == 0)
    die('No results');

// Iterate through results
while(($row = mysql_fetch_assoc($result)) {
    echo "User id: {$row['userid']}, Username: {$row['username']}<br />";
}
0

Hi, I ran this query in phpmyadmin and this worked, but above which you gave not fetched, it fetched wrong and with errors. as you can see the edits below.

SELECT `u`.`userid`, `u`.`username`, COUNT(*) AS `total_sales`
FROM `users` `u`
INNER JOIN `sales` `s` ON `s`.`userid` = `u`.`userid` AND `s`.`status` = 'delivered'
GROUP BY `u`.`userid`
ORDER BY `total_sales` DESC
LIMIT 10';

Now, I want to include that having count more than 10, thats killing me now

Edited by sammry

0

Wrap the query in double quotes, not single quotes.

Have you added the HAVING COUNT(*) > 10 back into the query and tested it in PHP MyAdmin again?

0

HI,

SELECT `u`.`userid`, `u`.`username`, COUNT(*) AS `total_sales`
FROM `users` `u`
INNER JOIN `sales` `s` ON `s`.`userid` = `u`.`userid` AND `s`.`status` = 'delivered'
GROUP BY `u`.`userid`
HAVING (COUNT(`s`.id) >= 10)
ORDER BY `total_sales` DESC
LIMIT 10';

this is querying the properly, thanks for tweaking and appreciate your time and help. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.