我有两张桌子,第一张桌子
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| pixel | text | NO | | NULL | |
| metadata | text | NO | | NULL | |
| data | text | NO | | NULL | |
| attended | int(11) | NO | | NULL | |
| alerted | int(11) | NO | | NULL | |
| date | bigint(20) | NO | | NULL | |
+----------+------------+------+-----+---------+----------------+
表二
+------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| lead_id | int(11) | NO | | NULL | |
| project_id | int(11) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| type | int(11) | NO | | NULL | |
| status | int(11) | NO | | NULL | |
| text | text | NO | | NULL | |
| comm | int(11) | NO | | NULL | |
| sum | text | NO | | NULL | |
| meeting_date | text | NO | | NULL | |
| meeting_location | text | NO | | NULL | |
| date | bigint(20) | NO | | NULL | |
+------------------+------------+------+-----+---------+----------------+
我想在1.id = 2.lead_id上连接第一到第二表,其中2.date是最大值 如果2.status等于x
,则选择它我试过,关于一切(接受答案:D),没有得到它。 谢谢。 这个问题不同它最后有一个AND cluse
答案 0 :(得分:0)
SELECT one.*, two.*
FROM one left join two on one.id=two.lead_id
WHERE MAX(two.date) and two.status='x'
答案 1 :(得分:0)
如果您只想将单个值的最小值/最大值作为条件,则可以使用ORDER BY
和LIMIT 0,1
来获取单行:)
SELECT *
FROM table_one
INNER JOIN (
SELECT *
FROM table_two
WHERE status = XXXXXXXXX
ORDER BY date DESC
LIMIT 0,1
) ON table_one.id = lead_id