带有JOIN和COUNT的MySQL

时间:2017-09-27 04:49:13

标签: mysql join

我有3张表如下:

markers
--->id
--->name

markers_date
--->id
--->markers_id
--->date
--->date2
--->time

registration
--->schedule_id

使用以下查询:

SELECT markers.name, markers_date.id, markers_date.markers_id, markers_date.date, markers_date.date2, markers_date.time, registration.schedule_id
FROM markers_date
JOIN markers ON markers_date.markers_id=markers.id
JOIN registration ON registration.schedule_id=markers_date.id

上面的查询输出正确,但是,我想计算有多少记录具有相同的“schedule_id”,并将该列放在“schedule_id”旁边。我已经尝试了下面的COUNT语句。但是,这给了我所有记录总数只有1个结果。目前有两条记录具有相同的“schedule_id”

SELECT markers.name, markers_date.id, markers_date.markers_id, markers_date.date, markers_date.date2, markers_date.time, registration.schedule_id, 
COUNT(registration.schedule_id=markers_date.id)
FROM markers_date
JOIN markers ON markers_date.markers_id=markers.id
JOIN registration ON registration.schedule_id=markers_date.id

下面的屏幕截图是没有

的情况
COUNT(registration.schedule_id=markers_date.id)

enter image description here

但下一个屏幕截图是我希望通过旁边的计数实现的,photoshop'd。

enter image description here

任何帮助将不胜感激。我已经在JOIN,INNER,OUTER等上看了很多youtube教程。但是,找不到任何带有COUNT的JOIN来协助我正在寻找的输出。

2 个答案:

答案 0 :(得分:1)

这应该有效:

    var express = require('express');
    var app = express();
    var path = require('path');
    var config = require('./server/config/db');
    var bodyParser = require('body-parser');

    app.use(bodyParser.json());

    app.use(bodyParser.urlencoded({ extended: false }));

    app.use(bodyParser.json({limit: '16mb'}));

    /*
        allow cross origin requests
    */
    app.use(function(req, res, next) {
        res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, 
        DELETE, GET");
        res.header("Access-Control-Allow-Origin", 
        "http://localhost:3000");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-
        With, Content-Type, Accept");
        res.header("Access-Control-Allow-Credentials", true);
        next();
    });
     /*
        FOR API ROUTES 
     */
    app.use('/', require('./server/routes/api'));
    /*
    FOR INDEX FILE PATH 
    */
    app.use('/root', express.static(path.join(__dirname, '/')));  
    app.use('/backDist',express.static(path.join(__dirname,
    '/admin/dist/')));    
    app.use('/dist',express.static(path.join(__dirname,'/client/dist/')));
    /*
        FOR GET IMAGE PATH 
    */
    app.use('/image',express.static(__dirname+'/admin/src/uploads'));
    /*
      FOR GET ROOT PATH 
    */ 
    app.get('/', function (req, res, err) {
      res.sendFile(path.join(__dirname+'/client/dist','index.html'))
    });
    app.get('/admin/*', function (req, res) {  
      res.sendFile(path.join(__dirname+'/admin/dist','index.html'))
    });
    app.get('/admin', function (req, res) { 
      res.sendFile(path.join(__dirname+'/admin/dist','index.html'))
    });
    /*
       FOR CONNECTION STATUS 
    */
    app.use(function(req, res, next) {  
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
    });

/*
    FOR SET CONNECTION PORT 
*/
app.listen(3000, function () {
console.log('Example listening on port 3000!');
});

module.exports = app; 

答案 1 :(得分:0)

以下内容未经过测试,但您可以使用GROUP BY

进行测试
SELECT markers.name, markers_date.id, markers_date.markers_id, markers_date.date, markers_date.date2, markers_date.time, registration.schedule_id, 
COUNT(registration.schedule_id) AS 'COUNT'
FROM markers_date
JOIN markers ON markers_date.markers_id=markers.id
JOIN registration ON registration.schedule_id=markers_date.id 
GROUP BY registration.schedule_id