简体   繁体   中英

Select distinct and random rows from one table that match a value from another table

This topic has been much discussed but I was unable to find a solution that I can modify and make it work for my case. So maybe a more advanced expert will be able to help out.

I have a table called keywords which contains about 3000 rows with distinct keywords. Against each keyword there is a matching product_id , which are NOT unique, ie some of them are repeated. Table looks something like this:

+---------+------------+
| keyword | product_id |
+---------+------------+
| apple1  | 15         |
| apple2  | 15         |
| pear    | 205        |
| cherry  | 307        |
| melon   | 5023       |
+---------+------------+

I have a second table called inventory that contains about 500K of products each with it's own product ID and other product data.

Now I need to get one random product row from inventory table that matches each product_id from keywords table and insert those rows into another table.

Resulting table should be something like this:

+---------+------------+---------+---------+---------+
| keyword | product_id | product |  data1  |  data2  |
+---------+------------+---------+---------+---------+
| apple1  | 15         | app5    |  d1     |  d2     |
| apple2  | 15         | app1    |  d1     |  d2     |
| pear    | 205        | pear53  |  d1     |  d2     |
| cherry  | 307        | cher74  |  d1     |  d2     |
| melon   | 5023       | melo2   |  d1     |  d2     |
+---------+------------+---------+---------+---------+

This is my query at the moment and the problem is how to get a random product from inventory that matches a product_id :

SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords LEFT OUTER JOIN
     inventory
     ON keywords.product_id = inventory.id            
ORDER BY RAND();

If you want it to only return rows when there is a match between the tables, then you want a regular (ie inner) join not a left outer join . You can also add the word distinct .

SELECT DISTINCT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
     inventory
     ON keywords.product_id = inventory.id            
ORDER BY RAND();

And if you only want 1 row returned, add limit 1 at the end.

SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
     inventory
     ON keywords.product_id = inventory.id            
ORDER BY RAND() LIMIT 1;

Is this what you want?

SELECT *
FROM (
  SELECT keywords.keyword, keywords.product_id, inventory.*
  FROM keywords JOIN
   inventory
   ON keywords.product_id = inventory.id            
  ORDER BY RAND()
) tmp
GROUP BY tmp.keyword;

I also test it at http://sqlfiddle.com/#!2/e559a9/2/0 . Just run some times, the result will be randomize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM