[英]How to get data with condition SQL QUERY
So first of all, I have 2 table.所以首先,我有 2 张桌子。 The first one is the categories
第一个是类别
Id Name
------------
1 Interior
2 Eksterior
3 Display
then the secon table is history which the data is the task I've finished然后第二个表是历史数据是我完成的任务
Id category_name category_id date (month) User Id
---------------------------------------------------------------
001 Interior 1 3 1084
002 Eksterior 2 3 1084
003 Interior 1 4 1089
004 Eksterior 2 4 1085
005 Display 3 4 1085
and what I want is to get categories by month, user id and know which one already done and not done from history, like this我想要的是按月获取类别,用户 ID 并知道哪些已经完成,哪些没有从历史记录中完成,就像这样
example the data in March with user id 1084:以用户 ID 为 1084 的 3 月份数据为例:
Id Name Status
---------------------------
1 Interior done
2 Eksterior done
3 Display not done
or like this:或像这样:
Id Name Status
--------------------------
1 Interior 1
2 Eksterior 1
3 Display 0
if the category in history table exist, the status will be 1 for done and 0 for not done.如果历史表中的类别存在,则状态将为 1 表示已完成,0 表示未完成。
this is my query before:这是我之前的查询:
SELECT c.id, c.category, c.id=h.category_id status FROM categories c, history h WHERE MONTH(h.created_at)
I keep retrieving the wrong result for my query.我一直在为我的查询检索错误的结果。 Please help me..
请帮我..
Seems like:看起来像:
SELECT *
FROM
categories c
LEFT JOIN history h on h.category_id = c.id AND h."date (month)" = 3
..will get you towards what you want: there will be NULL in the row from history table, for category Display; ..将使您朝着您想要的方向发展:历史表中的行中将有 NULL 用于显示类别; you can use this "is or is not null" to create your
done/not done
column你可以使用这个“is or is or not null”来创建你的
done/not done
列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.