How to get user all order_items from different orders by entityQuery I want to know if the user has purchased the specified product. I know the SQL. But I am learning to use entityQuery.
Example: user_id = 1, product_variant_id = 5
SELECT * 
FROM commerce_order 
    INNER JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id
WHERE (commerce_order.state = 'completed') 
    AND (commerce_order.uid = 1) 
    AND (commerce_order_item.purchased_entity = 5)
ORDER BY commerce_order.completed DESC

Could anyone help me tansfer to entityQuery?
Unfortunately, what you want to do isn't possible with an entity query, because that API does not support the appropriate joins. While you can simulate a join between a base entity and field value tables with field conditions, you cannot query against field values of referenced entities. If all you needed were order items referencing the order ID and specific purchasable entity ID, you'd be fine to write a single entity query against the order item table, but once you need to filter by order status (or support multiple purchasable entity types), you need to use \Drupal::database()-&gt;select().
To build a select query for this, I'd use the following:
$uid = 1;
$entity_id = 1;
$state = 'completed';

$query = \Drupal::database()-&gt;select('commerce_order', 'co');
$query-&gt;leftjoin('commerce_order_item', 'coi', 'co.order_id = coi.order_id');
$query-&gt;fields('co', ['order_id']);
$query-&gt;condition('co.state', $state);
$query-&gt;condition('co.uid', $uid);
$query-&gt;condition('coi.purchased_entity', $entity_id);
$result = $query-&gt;distinct()-&gt;countQuery()-&gt;execute()-&gt;fetchField();

The main change between your query and this code is that I'm fetching a count of the distinct number of orders. If you're trying to get the order IDs, remove the countQuery() and replace the fetchField() call with fetchAllKeyed(0, 0). If you really just want to search for a non zero count, you can remove the distinct() for faster query performance.