简体   繁体   中英

Data Listing According to Column From Table 1 to Table 2 in 2 Different SQL Tables

There are 2 different sql tables.

1.Table name: lottery

2.Table name: lottery_participant

1.Table (lottery), 2.Table "id" of table "lottery" (lottery_participant) 2. The table "lottery_id" depends on. As it appears in the picture.

What I want to do is I want to pull the data that has reached the number of data in the "lottery_participant" table based on the "max" value in the "lottery" table.

My SQL Code. (INCORRECT)

SELECT *,
CASE
    WHEN COUNT(P.id) >= L.max THEN NULL
END AS 'KISA AD' 
FROM lottery as L
INNER JOIN lottery_participant as P 
ON 
L.id = P.lottery_id
WHERE L.status= '0' 

If you want lotteries that are at or above the max:

SELECT l.*, lp.cnt
FROM lottery L JOIN
     (SELECT lp.lottery_id, COUNT(*) as cnt
      FROM lottery_participant lp
      GROUP BY lp.lottery_id
     ) lp
     ON l.id = lp.lottery_id
WHERE L.status = 0 AND cnt >= l.max

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