Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Mendapatkan prosedur tersimpan mySQL menghasilkan prosedur tersimpan lainnya

Tampaknya (per komunitas ini dan karena saya tidak dapat menemukan contoh prosedur tersimpan menggunakan set hasil dari prosedur tersimpan lainnya sebagai tabel di tabel pilih baru) bahwa mysql tidak mendukung ini atau sangat sulit untuk melakukan apa yang saya berharap untuk melakukannya.

Jadi, daripada memanggil prosedur tersimpan, saya menyalin kueri ke dalam prosedur terakhir ini.

    DELIMITER $$

    CREATE DEFINER=`root`@`localhost` PROCEDURE `get_team_stats_change`(
        IN team_id INT
    )
    BEGIN
    SELECT SUM(start_weight) AS start_weight, 
        SUM(start_body_fat) AS start_body_fat, 
        SUM(current_weight) AS current_weight,
        SUM(current_body_fat) AS current_body_fat,
        SUM(weight_change) AS weight_change, 
        SUM(body_fat_change) AS body_fat_change FROM (
    SELECT ls.person_id, 
        fs.weight AS start_weight, fs.body_fat AS start_body_fat, 
        ls.weight AS current_weight, ls.body_fat AS current_body_fat,
        fs.weight - ls.weight AS weight_change, fs.body_fat - ls.body_fat AS body_fat_change
    FROM
    (SELECT ttd.person_id, bs.weight, bs.body_fat, bs.test_date
    FROM body_stats AS bs
    JOIN
        ((SELECT pbs.person_id,  min(bs.test_date)  AS first_test, max(bs.test_date) AS last_test   
        FROM body_stats AS bs,
            person_body_stats AS pbs,
            team_member AS tm,
            team AS t
        WHERE t.team_id = team_id
        AND tm.team_id = t.team_id
        AND tm.person_id = pbs.person_id
        AND pbs.body_stats_id = bs.body_stats_id
        AND tm.start_date  bs.test_date
        AND bs.test_date >= t.starting_date
        GROUP BY person_id) AS ttd,
        person_body_stats AS pbs)
    ON bs.test_date = ttd.first_test 
        AND pbs.person_id = ttd.person_id 
        AND pbs.body_stats_id = bs.body_stats_id) AS fs,

    (SELECT ttd.person_id, bs.weight, bs.body_fat, bs.test_date
    FROM body_stats AS bs
    JOIN
        ((SELECT pbs.person_id,  min(bs.test_date)  AS first_test, max(bs.test_date) AS last_test   
        FROM body_stats AS bs,
            person_body_stats AS pbs,
            team_member AS tm,
            team AS t
        WHERE t.team_id = team_id
        AND tm.team_id = t.team_id
        AND tm.person_id = pbs.person_id
        AND pbs.body_stats_id = bs.body_stats_id
        AND tm.start_date  bs.test_date
        AND bs.test_date >= t.starting_date
        GROUP BY person_id) AS ttd,
        person_body_stats AS pbs)
    ON bs.test_date = ttd.last_test 
        AND pbs.person_id = ttd.person_id 
        AND pbs.body_stats_id = bs.body_stats_id) AS ls
        WHERE ls.person_id = fs.person_id
       ) AS delta;
    END



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Laravel fasih mendapatkan nilai paling umum di kolom database

  2. Mengubah konten div di Jquery Mobile

  3. Validasi Data MySQL saat Penyisipan

  4. Pencarian MySQL Fulltext Boolean Mode mengembalikan terlalu banyak hasil

  5. Impor database tunggal dari --all-databases dump