1
SELECT 
    * 
FROM `catalog_webdesign_products` t 
INNER JOIN tbl_member_registration t1 ont.userid=t1.fld_loginid 
WHERE 1 
    AND t1.fld_member_category_level<9 
    AND t.product_img IS NOT NULL  
ORDER BY RAND() LIMIT 5

在此查询中,我需要具有唯一用户 ID 的行。我也尝试过“按用户 ID 分组”,但结果集中出现了具有空值的 product_img。

4

2 回答 2

2

是的,你可以使用GROUP BY userid. 但是您的查询在这里看起来不对

   INNER JOIN tbl_member_registration t1 on t.userid=t1.fld_loginid
                                           ^------------------------space here

尝试这个

  SELECT 
  * 
  FROM `catalog_webdesign_products` t 
  INNER JOIN tbl_member_registration t1 on t.userid=t1.fld_loginid 
  WHERE  t1.fld_member_category_level<9 
  AND t.product_img IS NOT NULL  
  group by userid
  ORDER BY RAND() LIMIT 5

编辑:

 SELECT 
  * 
  FROM (select * from `catalog_webdesign_products` where product_img IS NOT NULL) t 
  INNER JOIN tbl_member_registration t1 on t.userid=t1.fld_loginid 
  WHERE  t1.fld_member_category_level<9 
  group by t.userid
  ORDER BY RAND() LIMIT 5
于 2013-01-10T11:06:39.447 回答
0

为了获得更好的性能,您应该将 t1 的条件移动到 join 语句中。您的问题可能是错误的 JOIN 条件。尝试 LEFT JOIN 而不是 INNER JOIN。

有关联接的更多信息:http: //www.w3schools.com/sql/sql_join.asp

SELECT * 
FROM `catalog_webdesign_products` t 
LEFT JOIN tbl_member_registration t1 
ON (t.userid = t1.fld_loginid AND t1.fld_member_category_level < 9)
WHERE t.product_img IS NOT NULL 
GROUP BY t.userid 
ORDER BY RAND() LIMIT 5
于 2013-01-10T11:42:06.273 回答