MySQL仅join最近一行?

我有一个表顾客存储customer_id,电子邮件和参考。 还有一个额外的表customer_data,用于存储对客户所做的更改的历史logging,即当插入新行时发生更改。

为了在表格中显示客户信息,需要将两个表连接起来,但只有customer_data中最近的一行应该连接到customer表。

查询分页会变得更复杂一些,所以有一个限制和一个偏移量。

我怎样才能做到这一点与MySQL? 我想我想把一个DISTINCT放在那里…

当时的查询就像这样 –

SELECT *, CONCAT(title,' ',forename,' ',surname) AS name FROM customer c INNER JOIN customer_data d on c.customer_id=d.customer_id WHERE name LIKE '%Smith%' LIMIT 10, 20 

另外,我是否正确地认为我可以像这样使用CONCAT?

(我很欣赏INNER JOIN可能是JOIN的错误types,实际上我不知道JOIN之间有什么区别,我现在要去看看!)

您可能想要尝试以下操作:

 SELECT CONCAT(title, ' ', forename, ' ', surname) AS name FROM customer c JOIN ( SELECT MAX(id) max_id, customer_id FROM customer_data GROUP BY customer_id ) c_max ON (c_max.customer_id = c.customer_id) JOIN customer_data cd ON (cd.id = c_max.max_id) WHERE CONCAT(title, ' ', forename, ' ', surname) LIKE '%Smith%' LIMIT 10, 20; 

请注意, JOIN只是INNER JOIN的同义词。

testing用例:

 CREATE TABLE customer (customer_id int); CREATE TABLE customer_data ( id int, customer_id int, title varchar(10), forename varchar(10), surname varchar(10) ); INSERT INTO customer VALUES (1); INSERT INTO customer VALUES (2); INSERT INTO customer VALUES (3); INSERT INTO customer_data VALUES (1, 1, 'Mr', 'Bobby', 'Smith'); INSERT INTO customer_data VALUES (2, 1, 'Mr', 'Bob', 'Smith'); INSERT INTO customer_data VALUES (3, 2, 'Mr', 'Jane', 'Green'); INSERT INTO customer_data VALUES (4, 2, 'Miss', 'Jane', 'Green'); INSERT INTO customer_data VALUES (5, 3, 'Dr', 'Jack', 'Black'); 

结果(查询没有LIMITWHERE ):

 SELECT CONCAT(title, ' ', forename, ' ', surname) AS name FROM customer c JOIN ( SELECT MAX(id) max_id, customer_id FROM customer_data GROUP BY customer_id ) c_max ON (c_max.customer_id = c.customer_id) JOIN customer_data cd ON (cd.id = c_max.max_id); +-----------------+ | name | +-----------------+ | Mr Bob Smith | | Miss Jane Green | | Dr Jack Black | +-----------------+ 3 rows in set (0.00 sec) 

如果您正在处理大量查询,则最好将请求移至where子句中的最新行。 这是更快,看起来更干净。

 SELECT c.*, FROM client AS c LEFT JOIN client_calling_history AS cch ON cch.client_id = c.client_id WHERE cch.cchid = ( SELECT MAX(cchid) FROM client_calling_history WHERE client_id = c.client_id AND cal_event_id = c.cal_event_id ) 

假设customer_data的autoincrement列被命名为Id ,您可以执行以下操作:

 SELECT CONCAT(title,' ',forename,' ',surname) AS name * FROM customer c INNER JOIN customer_data d ON c.customer_id=d.customer_id WHERE name LIKE '%Smith%' LIMIT 10, 20 And d.ID = ( Select Max(D2.Id) From customer_data As D2 Where D2.customer_id = D.customer_id ) 

对于任何必须使用旧版本的MySQL(5.0版之前的ish)的用户,您无法对此类查询进行子查询。 这是我能够做的解决scheme,它似乎很好。

 SELECT MAX(d.id), d2.*, CONCAT(title,' ',forename,' ',surname) AS name FROM customer AS c LEFT JOIN customer_data as d ON c.customer_id=d.customer_id LEFT JOIN customer_data as d2 ON d.id=d2.id WHERE CONCAT(title, ' ', forename, ' ', surname) LIKE '%Smith%' GROUP BY c.customer_id LIMIT 10, 20; 

基本上,这是find您的数据表的最大IDjoin到客户,然后join数据表到最大IDfind。 原因是因为select组的最大值不能保证其余的数据与id匹配,除非你将它加回到它自己。

我没有在新版本的MySQL上testing过,但它在4.0.30上工作。

 SELECT CONCAT(title,' ',forename,' ',surname) AS name * FROM customer c INNER JOIN customer_data d on c.id=d.customer_id WHERE name LIKE '%Smith%' 

我认为你需要将c.customer_id改为c.id

否则更新表结构

将实际数据logging到“ customer_data ”表中是个好主意。 有了这些数据,您可以根据需要从“customer_data”表中select所有数据。

这可能有帮助

如何从mysql表中select最新的一组datelogging

您可以使用子查询来获取最新的logging,然后join您的客户。