所以说我有一个包含以下列的数据库: $this->db->select('*');
$this->db->from($this->table1);
$this->db->join($this->table2, 'plot.location_id = locations.location_id');
$this->db->join($this->table3, 'plot.plot_type = plot_types.plot_id');
$this->db->join($this->table4, 'plot.user_id = admin.id');
$query = $this->db->get();
return $query->result();
,song_name
和artist
在album
我想要提取所有记录,并在groupA
我想要提取所有记录,但只提取groupB
和song_name
列。
这会给我一些类似的东西:
artist
groupA = Music.select("id, song_name, artist, album").uniq{ |e| [e.song_name, e.artist] }
groupB = Music.select("id, song_name, artist").uniq{ |e| [e.song_name, e.artist] }
结果:
groupA
[
[0] #<Music:0x007fc18a74b348> {
:id => "1",
:song_name => "Angie",
:artist => "The Rolling Stones",
:album => "Made In the Shade",
},
[1] #<Music:0x007fc18a0e1d90> {
:id => "2",
:song_name => "Beast of Burden",
:artist => "The Rolling Stones",
:album => "Some Girls",
},
[2] #<Music:0x007fc18a0e14f8> {
:id => "3",
:song_name => "Angie",
:artist => "The Rolling Stones",
:album => "Goats Head Soup",
}
]
结果:
groupB
我希望能够做的是从groupA中删除groupB并且只剩下差异。类似的东西:
[
[0] #<Music:0x007fc18a74b348> {
:id => "1",
:song_name => "Angie",
:artist => "The Rolling Stones",
},
[1] #<Music:0x007fc18a0e1d90> {
:id => "2",
:song_name => "Beast of Burden",
:artist => "The Rolling Stones",
}
]
- groupA
= groupB
:
groupC
另一个大问题是速度,可能会有数十万条记录返回,理想情况下,解决方案将是成本最低的方法。
答案 0 :(得分:1)
对于我想要实现的目标,这是一个不那么优雅的解决方案:
artists = Music.select("id, song_name, artist, album").map{|a| [a.id, a.song_name, a.artist, a.album]}
t1 = artists.uniq{ |e| [e[1], e[2], e[3]] }
puts "T1 Count: #{t1.count}"
t2 = t1.uniq{ |e| [e[1], e[2]] }
puts "T2 Count: #{t2.count}"
dups = t1 - t2
puts "dups: #{dups}"
# T1 Count: 3
# T2 Count: 2
# dups: [[3, "Angie", "The Rolling Stones", "Goats Head Soup"]]