简体   繁体   English

如何从一个表中选择所有行,并根据另一表计算字段值

[英]How can I select all rows from one table, calculating a field value based on another table

I have a table merchandises and merchandise_groups_merchandise . 我有一张桌子上的merchandisesmerchandise_groups_merchandise The latter table is the middle table for storing which merchandise is selected inside which merchandise groups - a many to many. 后一个表是中间表,用于存储在多个商品组中选择的商品(多对多)。 There's a relation from merchandises.id to merchandise_groups_merchandise.merchandise_id . merchandises.idmerchandise_groups_merchandise.merchandise_id有一个关系。

I'm trying to put together a query to produce the following results, listing all the merchandise from the merchandises table and where is_selected is a boolean decided by whether the merchandise_id is in the merchandise_groups_merchandise table for a specific merchandise_group. 我正在尝试汇总一个查询,以产生以下结果,列出merchandises表中的所有商品,并且is_selected是布尔值的布尔值,该布尔值由merchandise_id是否在特定merchandise_group的merchandise_groups_merchandise表中确定。 And, the merchandise_groups_merchandise.group_id is specified by the user. 并且, merchandise_groups_merchandise.group_id由用户指定。

I've tried a LEFT join, between the two tables, but of course, that only returns the actual merchandise that's in the group middle table. 我已经尝试在两个表之间进行LEFT联接,但是,当然,它只返回组中间表中的实际商品。

How can I do this? 我怎样才能做到这一点?

-------------------------------
|id  |title  |is_selected
---------------------------
|1   |Tree   |1
|2   |Log    |0
|3   |Toy    |1

This is the SQL of the query that I've tried: 这是我尝试过的查询的SQL:

PARAMETERS group_id Short;
SELECT IIf(IsNull([merchandise_groups_merchandise].[merchandise_id]),False,True) AS selected, merchandises.id, merchandises.title
FROM merchandises LEFT JOIN merchandise_groups_merchandise ON merchandises.id = merchandise_groups_merchandise.merchandise_id
WHERE (((merchandise_groups_merchandise.merchandise_group_id)=[group_id]));

One method is with iif() and an exists subquery: 一种方法是使用iif()exists一个子查询:

select m.*,
       iif(exists (select 1
                   from merchandise_groups_merchandise as mgm
                   where m.id = mgm.merchandise_id and
                         mgm.merchandise_group_id = [group_id]
                  )), 1, 0) as IsInGroupFlag
from merchandizes as m

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中选择行作为CSV并根据键插入另一个表? - How do I select rows from one table as CSV and insert into another table based on a key? 如何根据一个值从表一中获取 select,然后从同一表中获取 select 以及该表中的值? - How to select all from table one based on a value, then select from same table with value from that table? MySql根据另一个表中的MAX值选择一个表中的所有行 - MySql select all rows in one table based on MAX value in another table 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table 在SQL中,如何基于另一个字段的值将值从一个表复制到另一个表? - In SQL How do I copy values from one table to another based on another field's value? 如何在一个数据库表中查找其中一个字段的值与另一表中的值不同的行 - How to I find rows in one database table where a value of one field is NOT LIKE a value in another table 如何根据排名值从表中选择行 - How do I SELECT rows from a table based on a rank value 将一个表中的值与第二个表中字段的所有行相乘 - Multiply a Value from one table with all rows of a field on the second table Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition 如何从表中选择数据,在该表中我需要返回在一个字段中具有重复值而在另一个字段中具有指定值的行? - How do I select data from a table where I need to return rows which have a repeat value in one field and a specified value in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM