- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在类客户和类产品之间有很多关系。例如
数据库客户
| ID | Name |
-----------------------
| 01 | Jack |
| 02 | Joe |
| 03 | Claire |
数据库产品
| ID | ProductName |
---------------------------------
| 01 | watch |
| 02 | pen |
| 03 | car |
因此,在开发/构建之后,我得到一个名为 Customer_Products 的新数据库表,其中包含两个类的 ID。假设客户添加了一些产品,如下所示数据库客户_产品
| ID | CutomerID | PruductID |
----------------------------------------------------
| 01 | 01 | 01 | -> Jack added watch
| 02 | 02 | 01 | -> Joe added watch
| 03 | 01 | 02 | -> Jack added pen
| 04 | 01 | 03 | -> Jack added car
| 05 | 03 | 01 | -> Claire added watch
现在我只想显示添加了watch AND pen AND car 的客户(例如)。所以我想使用查询只显示 Jack,因为他添加了 3 种产品。这是我的问题。我的代码:
$sqlQuery = new SQLQuery();
$sqlQuery->setFrom("Customer");
$sqlQuery->addLeftJoin(' Customer_Products ','"Customer"."ID" = " Customer_Products "."CustomerID"');
$sqlQuery->addWhere("PruductID = 01 ");
// with this next two lines the result is empty, I know this is wrong but maybe this gives you an idea of what I m looking for.
// $sqlQuery->addWhere("PruductID = 02 ");
// $sqlQuery->addWhere("PruductID = 03 ");
$rawSQL = $sqlQuery->sql();
$rec = $sqlQuery->execute();
有没有一种方法可以通过一个查询来做到这一点?或者我怎么能做到这一点?感谢帮助!
最佳答案
有几种方法。我喜欢 group by
和 having
方法,因为它是最灵活的:
select c.*
from customers c join
customer_products cp
on c.customerid = cp.customerid join
products p
on p.productid = cp.productid
where p.name in ('Pen', 'Car', 'Watch')
group by c.customerid
having sum(p.name = 'Pen') > 0 and
sum(p.name = 'Car') > 0 and
sum(p.name = 'Watch') > 0;
having
子句中的每个条件都在测试一种产品的存在。 where
子句是可选的,但它可以帮助提高性能。
关于mysql - Silverstripe LeftJoin WHERE 两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467434/
代码在 EventUserTypes 模型中 $this->find() ->select(['event_usertypes.user_type_id' , 'usertypes.n
我的 eloquent 模型有一些问题,我尝试将两个不同的表合并为一个 现在我有两个表。 第一个表名称“company_saved_cv” |id|company_id|worker_id | ---
我正在尝试将 leftJoin 添加到 hasOne 方法中,但它似乎不起作用。来自主表的数据存在,但不来自连接表。代码: public function getUser() {
我是 laravel 的新手。 我有两张 table 。1)产品2) 价格 ----------------------------- - products - --
此代码返回“count”和“odds”值完全相同(它们不应该相同)。它实际上只计算“o.id”并为两者返回相同的值。 如何正确计算“b.id”? \DB::table('matches as
我有这个 SQL 代码,我想把它转换成 ormlite - 但我不知道如何以最好的方式做到这一点。 SELECT * FROM Job INNER JOIN Emp ON Job.JobAnsvar
我在使用 leftJoin 进行 laravel select 时遇到问题。我试图选择 2 个帖子并计算有多少条评论(第一个帖子有 7 个评论,第二个 - 0),但我只得到第一个帖子有 7 个评论。
我是 Laravel 的新手,正在学习 leftJoin。 我有一个名为“users”的表,其中行为“steam_id”。 我有另一个名为“匹配”的表。 “匹配”表有一个名为的唯一行“match_
我正在尝试使用此 Controller 删除“ super 管理员”: $user->superadmin = DB::table('role_users AS ru') ->leftJoin('us
这个问题在这里已经有了答案: Join between tables in two different databases? (4 个答案) 关闭 6 年前。 我有一个项目,其中有两个 MySQL
我在 MySQL 中有两个表。 logs 表包含列:time、user_id、event_type users 表有列:email, id 每次用户登录到生成日志文件的系统时,event_type 列
我的数据库中有以下表格: 连接 编号 owner_id 所有者 编号 名 ConnectionsController.php $connections = DB::table('connections
我有一个问题 Select m.name from machines m innerJoin config c ON m.cod = c.cod --> where ( c.categ
在我的 laravel 5.7/eloquent/Mysql 5.5 应用程序中,我需要在聊天列表中获取任何聊天消息的值以及给定 useronly ($user_id) 的任何聊天消息的另外 1 列消
我在类客户和类产品之间有很多关系。例如 数据库客户 | ID | Name | ----------------------- | 01 | Jack | | 02 | Joe | |
如何将以下查询转换为 Doctrine: SELECT restaurants.restaurant_name , restaurants.restaurant_id,
表 1) m_conservationsetting +------------+-------+------------+ | FacilityId | Unit | CategoryId | +
我有一个产品,其描述、名称等以多种语言保存在另一个表中。一种产品可以有多种翻译(产品数据)。 目前,我获得了产品的所有翻译,并执行了 foreach 和 ifs 来根据语言环境获得所需的翻译...如果
我有 3 个模型: 用户 喜欢 发帖 Like 在 user_id 和 post_id(这是其他 2 个模型的 id 列)上使用 CakePHP 约定设置了 2 个外键。 我想运行一个查找,在 Pos
旧的 leftJoin 问题的一个细微变化:我有两个要加入的表: ID Val1 1 100 2 200 3 300 和 ID Val2 2 50 已关注 combine two tables
我是一名优秀的程序员,十分优秀!