Sql 如何计算一个表中的值,然后在另一个表中显示相同的值?
我想计算特定购物列表的Sql 如何计算一个表中的值,然后在另一个表中显示相同的值?,sql,ruby-on-rails,database,postgresql,Sql,Ruby On Rails,Database,Postgresql,我想计算特定购物列表的产品总数,然后根据购物列表表中的id显示该购物列表的产品总数。我已在产品表中为购物列表创建了一个参考 我的购物清单如下:- id serial NOT NULL, shopping_list_name character varying(255), shopping_list_status character varying(255) DEFAULT 'OPEN'::character varying, total_items character varying
产品总数
,然后根据购物列表表中的id显示该购物列表的产品总数
。我已在产品表中为购物列表创建了一个参考
我的购物清单如下:-
id serial NOT NULL,
shopping_list_name character varying(255),
shopping_list_status character varying(255) DEFAULT 'OPEN'::character varying,
total_items character varying(255) DEFAULT 0,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
deleted integer NOT NULL DEFAULT 0,
CONSTRAINT shopping_lists_pkey PRIMARY KEY (id)
id serial NOT NULL,
shopping_list_id integer NOT NULL,
product_name character varying(255) DEFAULT 'null'::character varying,
product_category character varying(255),
quantity integer,
status character varying(255) DEFAULT 'OPEN'::character varying,
deleted integer NOT NULL DEFAULT 0,
CONSTRAINT products_pkey PRIMARY KEY (id)
我的产品表如下:-
id serial NOT NULL,
shopping_list_name character varying(255),
shopping_list_status character varying(255) DEFAULT 'OPEN'::character varying,
total_items character varying(255) DEFAULT 0,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
deleted integer NOT NULL DEFAULT 0,
CONSTRAINT shopping_lists_pkey PRIMARY KEY (id)
id serial NOT NULL,
shopping_list_id integer NOT NULL,
product_name character varying(255) DEFAULT 'null'::character varying,
product_category character varying(255),
quantity integer,
status character varying(255) DEFAULT 'OPEN'::character varying,
deleted integer NOT NULL DEFAULT 0,
CONSTRAINT products_pkey PRIMARY KEY (id)
有人能帮我吗:-这样你就有了
class ShoppingList < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :shopping_list
end
class Product < ActiveRecord::Base
after_create :update_shopping_list
def update_shopping_list
shopping_list.update_attribute(:products_quantity, shopping_list.products_quantity + self.quantity)
end
end
并得到数量
@my_shopping_list.products.sum(:quantity)
您可以在购物列表上有某种缓存,因此假设您的购物列表上有一个名为products\u quantity的整数字段,默认值为0(确保默认值为0,而不是nil)
然后在您的产品模型上,您可以执行以下操作
class ShoppingList < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :shopping_list
end
class Product < ActiveRecord::Base
after_create :update_shopping_list
def update_shopping_list
shopping_list.update_attribute(:products_quantity, shopping_list.products_quantity + self.quantity)
end
end
类产品
在这种情况下,您不必执行查询selectsum(quantity)fromsproducts,其中shopping\u list\u id=
我不确定我是否回答了你的第二个问题,但是如果你有更多的问题或者你想问别的问题,请告诉我。那么你会有类似的问题
class ShoppingList < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :shopping_list
end
class Product < ActiveRecord::Base
after_create :update_shopping_list
def update_shopping_list
shopping_list.update_attribute(:products_quantity, shopping_list.products_quantity + self.quantity)
end
end
并得到数量
@my_shopping_list.products.sum(:quantity)
您可以在购物列表上有某种缓存,因此假设您的购物列表上有一个名为products\u quantity的整数字段,默认值为0(确保默认值为0,而不是nil)
然后在您的产品模型上,您可以执行以下操作
class ShoppingList < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :shopping_list
end
class Product < ActiveRecord::Base
after_create :update_shopping_list
def update_shopping_list
shopping_list.update_attribute(:products_quantity, shopping_list.products_quantity + self.quantity)
end
end
类产品
在这种情况下,您不必执行查询selectsum(quantity)fromsproducts,其中shopping\u list\u id=
我不确定我是否回答了你的第二个问题,但是如果你有更多的问题或者你想问其他问题,请告诉我。听起来你只是想做一个选择shopping.id,count(product.id),sum(product.quantity)from shopping internal join product on(…)。。。按购物分组。id
@CraigRinger这正是我想要做的。你能帮我了解一下细节吗?基本上就是这些细节。如果你的意思是“你能把它写成一个ActiveRecord查询吗?”那么不,对不起,我不做ActiveRecord,但是你可能可以从SQL中把它翻译回来。听起来你只是想做一个选择shopping.id,count(product.id),sum(product.quantity)from shopping in join product on(…)。。。按购物分组。id
@CraigRinger这正是我想要做的。你能帮我了解一下细节吗?基本上就是这些细节。如果你的意思是“你能把它写成ActiveRecord查询吗?”那么不,对不起,我不做ActiveRecord,但你可以从SQL中把它翻译回来。我能直接在数据库@rorra中更改数量的值吗,谢谢你的帮助。我按照您描述的方式实现了我的购物车,它正在工作。我可以直接在数据库@rorra中更改数量的值吗?谢谢您的帮助。我按照您描述的方式实现了我的购物车,它正在工作