如果标题没有任何意义,那么我需要在坚果壳中做些什么......我需要选择最近的X量记录"按日期"在主表中,然后通过选择最新记录" by id"来加入属于这些记录的数据。在联合表中..
这里有一些示例输出..
table:lead_unique(此表中只有唯一的ssn)
+-----------+--------------+ | ssn | created_date | +-----------+--------------+ | 111111111 | 2015-03-01 | | 999999999 | 2015-03-03 | | 555555555 | 2015-02-08 | +-----------+--------------+
表:lead_data
+----+-----------+-------+----------------+-------------+-------+-------+ | id | ssn | name | address | city | state | zip | +----+-----------+-------+----------------+-------------+-------+-------+ | 1 | 111111111 | Bob1 | 1234 Test Ln | Mound | CA | 55555 | | 2 | 111111111 | Bob2 | 1234 Test Ln | Mound | CA | 55555 | | 3 | 999999999 | Jane1 | 5432 Lola Blvd | Patton | NJ | 33333 | | 4 | 999999999 | Jane2 | 5432 Lola Blvd | Patton | NJ | 33333 | | 5 | 555555555 | Jack1 | 832 92nd Ave N | Bright View | AL | 88888 | | 6 | 999999999 | Jane3 | 5432 Lola Blvd | Patton | NJ | 33333 | +----+-----------+-------+----------------+-------------+-------+-------+
所需的输出(可以是asc / desc日期列,不重要)
+--------------+-----------+-------+ | created_date | ssn | name | +--------------+-----------+-------+ | 2015-03-03 | 999999999 | Jane3 | | 2015-03-01 | 111111111 | Bob2 | | 2015-02-08 | 555555555 | Jack1 | +--------------+-----------+-------+
期望的输出(限制2)
+--------------+-----------+-------+ | created_date | ssn | name | +--------------+-----------+-------+ | 2015-03-03 | 999999999 | Jane3 | | 2015-03-01 | 111111111 | Bob2 | +--------------+-----------+-------+
查询可能类似于以下内容,但我也可能因为我在这里寻求帮助而没有运气而感到害怕..
SELECT lead_unique.created_date, lead_unique.ssn, lead_data.name FROM lead_unique JOIN ( SELECT ... FROM lead_data ... ) lead_data ... ... LIMIT 2
我之前只使用了一次堆栈溢出,所以如果有更多我可以添加有用的请告诉我!谢谢!!
答案 0 :(得分:0)
我倾向于使用相关子查询来处理数据 - 您的问题只提到一列:
select u.created_date, u.ssn,
(select d.name
from lead_data d
where d.ssn = u.ssn
order by d.id desc
limit 1
) as name
from lead_unique u
order by u.created_date desc
limit 2;
实际上,出于性能原因,我会将唯一组件放在子查询中:
select u.created_date, u.ssn,
(select d.name
from lead_data d
where d.ssn = u.ssn
order by d.id desc
limit 1
) as name
from (select u.*
from lead_unique u
order by u.created_date desc
limit 2
) u;
编辑:
即使有多列,最高性能的方法仍然可能是使用子查询:
select created_date, ssn, d.*
from (select u.created_date, u.ssn,
(select d.id
from lead_data d
where d.ssn = u.ssn
order by d.id desc
limit 1
) as id
from (select u.*
from lead_unique u
order by u.created_date desc
limit 2
) u
) u join
lead_data d
on u.id = d.id;
顺便说一下,如果性能问题,您需要以下索引:lead_unique(created_date)
和lead_data(id)
。这两个索引应该非常快。