Magento: Get users who has bought this product

In this article I'm writing code how you get users who has bought this products. You can achieve this by magento models. If you want an actual query, you can probably do something as simple as (add additional joins to get customer information from EAV). You have to base your query on orders. If you want to do it by SQL query, you have to it by the following table: 1) sales_flat_quote, sales_flat_order_item to get the link between customer and product 2) catalog_category_product to get the link between category and produc 3) catalog_product_entity to get the product id in function of the sku.

SELECT DISTINCT o.customer_id FROM sales_flat_order_item i
INNER JOIN sales_flat_order o ON o.entity_id = i.order_id
WHERE o.customer_id IS NOT NULL
AND i.sku = 'product-sku'

Create a file in root magento directory and paste below code.

<?php

require_once 'app/Mage.php';

/*
 * Initialize Magento. Older versions may require Mage::app() instead.
 */
Mage::init();

/**
 * Get all unique order IDs for items with a particular SKU.
 */
$orderItems = Mage::getResourceModel('sales/order_item_collection')
    ->addFieldToFilter('sku', 'some-product-sku')
    ->toArray(array('order_id'));

$orderIds = array_unique(array_map(
    function($orderItem) {
        return $orderItem['order_id'];
    },
    $orderItems['items']
));

/**
 * Now get all unique customers from the orders of these items.
 */
$orderCollection = Mage::getResourceModel('sales/order_collection')
    ->addFieldToFilter('entity_id',   array('in'  => $orderIds))
    ->addFieldToFilter('customer_id', array('neq' => 'NULL'));
$orderCollection->getSelect()->group('customer_id');

/**
 * Now get a customer collection for those customers.
 */
$customerCollection = Mage::getModel('customer/customer')->getCollection()
    ->addFieldToFilter('entity_id', array('in' => $order->getColumnValues('customer_id')));

/**
 * Traverse the customers like any other collection.
 */
foreach ($customerCollection as $customer) {
    var_dump($customer->getData());
}