如何在mysql、cakephp3.6中找到多对多关系中的count

c9qzyr3d  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(290)

我需要计算每个类别的计数(vendors.id)。ie:每个类别(不是子类别)有多少供应商
我使用cakephp3.6框架和mysql作为数据库
我尝试了所有可能的方法,我知道,但没有找到任何解决办法。有人能帮我吗,这对我的项目很重要
[更新]我使用的sql查询:

  1. SELECT cat.id,cat.name ,COUNT(`vendor_id`) AS vendor_count FROM `vendor_services` JOIN `categories` ON(`vendor_services`.`category_id` = `categories`.`id`) JOIN `categories` AS cat ON(categories.category_id = cat.id) WHERE 1 GROUP BY cat.id

[更新]下面是创建相应表的sql

  1. -- phpMyAdmin SQL Dump
  2. -- version 4.7.0
  3. -- https://www.phpmyadmin.net/
  4. --
  5. -- Host: 127.0.0.1
  6. -- Generation Time: Dec 04, 2018 at 10:50 AM
  7. -- Server version: 10.1.24-MariaDB
  8. -- PHP Version: 7.1.6
  9. SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  10. SET AUTOCOMMIT = 0;
  11. START TRANSACTION;
  12. SET time_zone = "+00:00";
  13. --
  14. -- Database: `demo123`
  15. --
  16. -- --------------------------------------------------------
  17. --
  18. -- Table structure for table `categories`
  19. --
  20. CREATE TABLE `categories` (
  21. `id` int(11) NOT NULL,
  22. `category_id` tinyint(4) NOT NULL,
  23. `name` varchar(60) NOT NULL
  24. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  25. --
  26. -- Dumping data for table `categories`
  27. --
  28. INSERT INTO `categories` (`id`, `category_id`, `name`) VALUES
  29. (1, 0, 'Books'),
  30. (2, 0, 'Electronics'),
  31. (3, 0, 'Garden'),
  32. (4, 1, 'Novel'),
  33. (5, 1, 'Science'),
  34. (6, 1, 'Story'),
  35. (7, 2, 'Mobile'),
  36. (8, 2, 'Tablet'),
  37. (9, 2, 'Headphone'),
  38. (10, 3, 'Pumps'),
  39. (11, 3, 'Pipes');
  40. -- --------------------------------------------------------
  41. --
  42. -- Table structure for table `vendors`
  43. --
  44. CREATE TABLE `vendors` (
  45. `id` int(11) NOT NULL,
  46. `name` varchar(60) NOT NULL
  47. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  48. --
  49. -- Dumping data for table `vendors`
  50. --
  51. INSERT INTO `vendors` (`id`, `name`) VALUES
  52. (1, 'VR Enterprizes'),
  53. (2, 'RR Vendors'),
  54. (3, 'JK Suppliers');
  55. -- --------------------------------------------------------
  56. --
  57. -- Table structure for table `vendor_services`
  58. --
  59. CREATE TABLE `vendor_services` (
  60. `id` int(11) NOT NULL,
  61. `vendor_id` int(11) NOT NULL,
  62. `category_id` int(11) NOT NULL
  63. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  64. --
  65. -- Dumping data for table `vendor_services`
  66. --
  67. INSERT INTO `vendor_services` (`id`, `vendor_id`, `category_id`) VALUES
  68. (1, 1, 4),
  69. (2, 1, 5),
  70. (3, 1, 6),
  71. (4, 1, 11),
  72. (5, 2, 7),
  73. (6, 2, 8),
  74. (7, 2, 9),
  75. (8, 3, 10),
  76. (9, 3, 11);
  77. --
  78. -- Indexes for dumped tables
  79. --
  80. --
  81. -- Indexes for table `categories`
  82. --
  83. ALTER TABLE `categories`
  84. ADD PRIMARY KEY (`id`),
  85. ADD KEY `category_id` (`category_id`);
  86. --
  87. -- Indexes for table `vendors`
  88. --
  89. ALTER TABLE `vendors`
  90. ADD PRIMARY KEY (`id`);
  91. --
  92. -- Indexes for table `vendor_services`
  93. --
  94. ALTER TABLE `vendor_services`
  95. ADD PRIMARY KEY (`id`),
  96. ADD KEY `vendor_id` (`vendor_id`),
  97. ADD KEY `category_id` (`category_id`);
  98. --
  99. -- AUTO_INCREMENT for dumped tables
  100. --
  101. --
  102. -- AUTO_INCREMENT for table `categories`
  103. --
  104. ALTER TABLE `categories`
  105. MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
  106. --
  107. -- AUTO_INCREMENT for table `vendors`
  108. --
  109. ALTER TABLE `vendors`
  110. MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
  111. --
  112. -- AUTO_INCREMENT for table `vendor_services`
  113. --
  114. ALTER TABLE `vendor_services`
  115. MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;COMMIT;

可分类的

  1. $this->hasMany(‘Subcategories’, [ 'className'=> Categories’,
  2. 'foreignKey' => 'category_id',
  3. 'conditions' => ['category_id!= 0']
  4. ]);
  5. $this->belongsTo('MainCategories', [
  6. 'className'=> Categories’,
  7. 'foreignKey' => 'category_id',
  8. ]);

ncgqoxb0

ncgqoxb01#

下面是oracle中的查询,请看一看,按照mysql进行修改,
在插入下面添加,插入到供应商服务(id、供应商id、类别id)值(11、3、3);
选择frm.category\u id、frm.vendor\u id、frm.vendor\u name、count(vendor\u id)计数来自(选择不同的ct.category\u id、vs.vendor\u id、vs.category\u id vs.category\u id、vn.id、vn.name vendor\u name from categegories ct inner join vendor\u services vs on vs.category\u id=ct.category\u id inner join vendors vn on vs.vendor\u id=vn.id)frm group by category\u id,供应商id、供应商名称

相关问题