比较多行和多列

时间:2014-11-12 04:30:47

标签: mysql

faction_id   marker_id    cycle_id
14           1             1
14           2             1
12           2             2
12           3             2

我试图用cycle_id 2抓住两个条目,忽略第1个循环中的标记2,但仍然在第1个循环中返回标记1.这样我就可以显示所有当前数据(最近的循环)并且只显示标记在之前的周期中没有改变faction_id。

现在,我只是返回整个表,基于cycle_id对它进行排序,并在返回数据时覆盖PHP数组中的旧循环数据,但当数据库变大时,这不是一种有效的处理方法。

我当前的选择声明: (所有这些列都在tok_ownership中)

SELECT tok_marker.id as id,
 tok_marker.name as name,
 tok_marker.lat as lat, 
 tok_marker.lng as lng, 
 tok_faction.colour as colour, 
 tok_ownership.war_conditions as war_conditions, 
 tok_ownership.improvements as improvements, 
 tok_ownership.poa as poa 
 FROM tok_ownership 
 JOIN tok_marker ON tok_marker.id=tok_ownership.marker_id 
 JOIN tok_faction ON tok_faction.term_id=tok_ownership.faction_id 
 ORDER BY tok_ownership.cycle_id ASC;

tok_ownership

1   marker_id   mediumint(8)
2   faction_id  mediumint(8)
3   cycle_id    mediumint(8)
4   victory_id  smallint(6)
5   war_conditions  varchar(64) 
6   improvements    varchar(64)
7   active_effects  text
8   poa tinyint(3)

tok_marker

1   id  mediumint(8)
2   name    varchar(64)
3   lat decimal(10,7)
4   lng decimal(10,7)

tok_faction

1   term_id smallint(6)
2   colour  char(6)

tok_cycle

1   id  int(10)
2   week    date

3 个答案:

答案 0 :(得分:1)

我认为这是一种简单的方法

select tok_marker.id,
 tok_marker.name as name,
 tok_marker.lat as lat, 
 tok_marker.lng as lng, 
 tok_faction.colour as colour, 
 tok_ownership.war_conditions as war_conditions, 
 tok_ownership.improvements as improvements, 
 tok_ownership.poa as poa,
 max(cycle_id)
 FROM tok_ownership 
 JOIN tok_marker ON tok_marker.id=tok_ownership.marker_id 
 JOIN tok_faction ON tok_faction.term_id=tok_ownership.faction_id 
 group tok_marker.id

答案 1 :(得分:0)

您的查询中缺少条件。条件通常使用WHERE关键字提供。

例如:

WHERE tok_ownership.cycle_id = 2 AND tok_ownership.marker_id != 2

这将选择cycle_id等于2且marker_id 等于2的行。

答案 2 :(得分:0)

今天以新鲜的大脑来到我身边,我设法把它放在一起。

SELECT * FROM
(SELECT tok_ownership.marker_id as marker,
term_data.name as owner,
tok_marker.name as name, 
tok_marker.lat as lat,
tok_marker.lng as lng,
tok_faction.colour as colour,
tok_ownership.war_conditions as war_conditions,
tok_ownership.improvements as improvements,
tok_ownership.poa as poa,
tok_ownership.cycle_id as cycle 
FROM tok_ownership 
JOIN term_data ON term_data.tid=tok_ownership.faction_id 
JOIN tok_marker ON tok_marker.id=tok_ownership.marker_id 
JOIN tok_faction ON tok_faction.term_id=tok_ownership.faction_id 
WHERE cycle_id <= 2 ORDER BY tok_ownership.cycle_id DESC, tok_marker.id ASC) cycles 
GROUP BY cycles.marker;

这将返回带有cycle_id 1的标记1,带有cycle_id 2的标记2和3,但不会返回带有cycle_id 1的标记2.并且可以使用PHP中的变量轻松调整。谢谢大家的帮助,并试图了解我昨天过度劳累的大脑!

更新:我也做了以下声明,它做了同样的事情。这会比第一个更好地优化吗?

SELECT term_data.name as owner,
tok_marker.id as marker, 
tok_marker.name as name, 
tok_marker.lat as lat, 
tok_marker.lng as lng,  
tok_faction.colour as colour, 
tok_ownership.war_conditions as war_conditions,  
tok_ownership.improvements as improvements, 
tok_ownership.poa as poa,  
tok_ownership.cycle_id as cycle 
FROM tok_ownership 
JOIN term_data ON tok_ownership.faction_id=term_data.tid 
JOIN tok_marker ON tok_marker.id=tok_ownership.marker_id 
JOIN tok_faction ON tok_faction.term_id=tok_ownership.faction_id 
JOIN (SELECT id, cycle_id FROM 
 (SELECT tok_marker.id,
  tok_ownership.cycle_id 
  FROM tok_marker 
  JOIN tok_ownership ON tok_ownership.marker_id=tok_marker.id 
  WHERE tok_ownership.cycle_id <= 3 
  ORDER BY tok_ownership.cycle_id DESC, tok_marker.id DESC) ids 
 GROUP BY ids.id 
 ORDER BY ids.id DESC) marker_list ON tok_marker.id=marker_list.id 
 AND tok_ownership.cycle_id=marker_list.cycle_id;