简体   繁体   English

在查询时将变量设置为true或false

[英]setting variable to true or false at query time mysql

I'm in a bit of a dilemma here and would appreciate opinions 我在这里有些两难,请多多指教

I have the following schema 我有以下架构

User{id,name}
Class{id}
textbook{id,name,public}
class_textbook{id,class_id,textbook_id}
class_membership{id,user_id,class_id}

textbook.public is a boolean field marking if a textbook is available for download by every student or only by students participating in a class to which the book is assigned textbook.public是一个布尔值字段,用于标记每个学生是否都可以下载教科书,或者只有参与该课本的班级的学生可以下载教科书

I want to select every textbook which is available to the user (either public or assigned in a class the user participates) but I have to know which book is public, which isn't and which class is taught with a public textbook. 我想选择用户可用的每本教科书(公开的或用户参加的班级分配的教科书),但是我必须知道哪本是公开的,哪本不是公开的,以及使用公开教科书教的是哪一类。

One way to solve this is by running two queries One to get every textbook available to the user and one to find the public ones 解决此问题的一种方法是运行两个查询,一个查询使每本课本可供用户使用,另一个查询公共的课本。

Or i could define a mysql function to do this for me. 或者我可以定义一个mysql函数来为我做这个。

SELECT 
    * 
FROM textbook 
WHERE id in (SELECT textbook_id FROM class_textbook WHERE class_id IN (SELECT class_id FROM class_membership WHERE user_id = :user_id)) 
   OR public = true

where :user_id is the id number associated with the user in question. 其中:user_id是与相关用户关联的ID号。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM