Select 一个 table 列具有与其他 table 所有数据不同的记录

Select one table column with distinct record with other table all data

我有 2 个 table 'userfoodcategory' 和 'MenuMaster'。

'userfoodcategory' 有食品类别,'MenuMaster' 有多个项目以及此类别的列 'isnonveg'。

我想查询 'userfoodcategory' table 数据,其中有 1 个附加列 'isnonveg',该列在 'MenuMaster' table 中。

我正在尝试以下查询,但它给出了冗余记录

 SELECT DISTINCT ufc.*, MM.isnonveg
FROM   MenuMaster MM
LEFT JOIN  userfoodcategory ufc  ON MM.categoryid = ufc.foodcategoryid
WHERE  ufc.USERID = 19 --and MM.isnonveg IS NULL
order by ufc.foodcategoryid

有关详细信息,请查看下面的屏幕截图。

我也想把它作为一个 linq 查询,但首先我试图在 sql 中构建它,然后我需要在 linq 中转换它,因为我是 linq 的新手。 提前致谢。

您可以尝试使用以下查询:

SELECT DISTINCT ufc.*, MM.isnonveg
FROM  (select distinct categoryid,isnonveg FROM MenuMaster) MM
LEFT JOIN  userfoodcategory ufc  ON MM.categoryid = ufc.foodcategoryid
WHERE  ufc.USERID = 19 --and MM.isnonveg IS NULL
order by ufc.foodcategoryid