简体   繁体   中英

joining two queries for different tables

I want to find the first 4 sponsors that appears highest in my referral table and sum up bonus for each of them in my referral table, and echo the sponsor, number of times it appears, and the sum of the bonus from the referral table.

This is my database schema for the two tables

$bonuses = "CREATE TABLE IF NOT EXISTS bonuses (
    userid int (11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    sponsor VARCHAR (16) NOT NULL,
    username VARCHAR (20) NOT NULL,
    bonus int (6) NOT NULL,
    bonusid VARCHAR (5) NOT NULL,
    time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status int (1) NOT NULL DEFAULT '0'
   )ENGINE=InnoDB DEFAULT CHARSET= latin1";

$query = mysqli_query($conn, $bonuses);

$referrals = "CREATE TABLE IF NOT EXISTS referrals (
    userid int (11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    sponsor VARCHAR (16) NOT NULL,
    username VARCHAR (20) NOT NULL,
    phoneNumber VARCHAR (11) NOT NULL,
    email VARCHAR (50) NOT NULL,
    reg_Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    totalbonus int (7) NOT NULL DEFAULT '0',
    status int (1) NOT NULL DEFAULT '1',
    UNIQUE KEY (username)
   )ENGINE=InnoDB DEFAULT CHARSET= latin1";

$query = mysqli_query($conn, $referrals);

<?php
$query=mysqli_query($conn, "SELECT sponsor, COUNT(sponsor) AS numref FROM referrals  where not sponsor='admin' GROUP BY sponsor ORDER BY numref DESC LIMIT 4");
            echo '<div style="font-size:20px; font-weight:bold; color: #fff; background-color:#800040; padding: 5px; border-radius: 10px;
            box-shadow:2px 2px 2px 2px #aaa; margin-bottom:2px;">WPM Award Statistics(Top 4)</div>';
            echo '<div class="table-responsive">';
            echo "<table class='table table-bordered table-hover table-striped'>";
            echo '<thead style="color:#800040;"><tr><th>Sponsor</th><th>Num of Referrals</th></tr></thead>';
        while ($result = mysqli_fetch_array($query)) {
            $sponsor=$result['sponsor'];
            $numref=$result['numref'];
            echo '<tbody><tr><td>', "$sponsor", '</td><td>', "$numref", '</td></tr></tbody>';

        }
            echo '</table></div>';
?>

this is what I have already for the sponsor and number of referral table

I want to echo a 3rd column containing the sum of the bonuses for each of these sponsors where status=3

SELECT ref.sponsor, COUNT(ref.sponsor) AS numref, sum(bns.bonus) as bonuses 
FROM referrals ref
LEFT JOIN bonuses bns ON bns.sponsor = ref.sponsor
WHERE NOT ref.sponsor='admin' AND bns.status = 3
GROUP BY ref.sponsor 
ORDER BY numref DESC 
LIMIT 4

OR

SELECT ref.sponsor, COUNT(ref.sponsor) AS numref, 
(SELECT sum(bonus) FROM bonuses WHERE sponsor = ref.sponsor AND status = 3) AS bonuses
FROM referrals ref
WHERE NOT ref.sponsor='admin'
GROUP BY ref.sponsor 
ORDER BY numref DESC 
LIMIT 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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