我有一张带报价的表格,其中存储了商人的报价(价格,评论,雇主接受此报价的日期......),还有另一张评级表(正面/负面,评论,评级日期已提交, ......)每个商人。因此,一个商人可以有0..n引号和0..n评级。
我想知道的是什么:
我目前的做法:
第一个查询('报价')是找出报价的用户ID以及雇主接受报价的日期。
第二个查询('rating')在一个区块内。对于每个商人ID,查询应该获取与报价被接受的日期相同或相似的评级。
问题:
代码:
#!/usr/bin/ruby
require "mysql"
begin
db_q = Mysql.real_connect("localhost", "root", "", "my_db")
quotes = db_q.query("
SELECT q.user_id, q.accepted_at
FROM quotes q
INNER JOIN jobs j ON q.job_id = j.id
INNER JOIN categories_jobs cj ON j.id = cj.job_id
INNER JOIN categories c ON cj.category_id = c.id
INNER JOIN users u ON j.user_id = u.id
WHERE q.accepted_at IS NOT NULL
AND c.id = 8
")
db_r = Mysql.real_connect("localhost", "root", "", "my_db")
quotes.each do |quote|
ratings = db_r.query("SELECT * FROM ratings WHERE assessee_id = #{quote[0]} AND CAST(created_at as DATE) <= '#{quote[1]}'")
ratings.each do |rating|
printf "%s, %s, %s, %s, %s, %s\n", rating[0], rating[2], rating[3], rating[4], rating[5], rating[6]
end
end
ratings.free
quotes.free
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
# disconnect from server
db_q.close if db_q
db_r.close if db_r
end