Hi,
I'm trying to create a query of table of purchase records to return only users who have made a single purchase, so basically it would return one-time purchasers. Every purchaser is assigned a unique id number (userid), so i've been trying to come up with a quick and simple query that searches for unique userids in the sales_record table. I can put together a script to do it the hard way (ie: do an initial query of the userid table, then query the sales_record table for each userid and only print out the rows returned where mysql_num_rows == 1) but i was wondering if anyone knew of a more elegant/quick way to do it.
Thanks!
Query MySql for User with Single Record
Moderator: General Moderators
Re: Query MySql for User with Single Record
Hmm..
How does that work?
Code: Select all
SELECT count(*) AS `numpurchased`, `user_id` FROM `sales_record_table` HAVING `numpurchased` = 1 GROUP BY `user_id`Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: Query MySql for User with Single Record
Without knowing the fields..
Code: Select all
SELECT
m.first_name,
m.last_name,
m.user_id
FROM
members m
WHERE
(SELECT COUNT(*) FROM orders o WHERE m.user_id = o.user_id) = 1
ORDER BY
last_name, first_name ASC