简体   繁体   English

这可以在1个mysql查询中做吗

[英]Is this possible to do in 1 mysql query

I'm trying to get all the app names from this table 我正在尝试从此表中获取所有应用程序名称

SELECT id, name FROM app_names

And using that data fetch these results 并使用该数据获取这些结果

SELECT count(*) FROM users_dailyrecords WHERE day='x' appName='THE ID FROM THE SELECT ABOVE'

And with the result of that query I want to insert it into 并与该查询的结果,我想将其插入

INSERT INTO app_stats (appID, day, totalUsers) VALUES ('appID', 'x', 'RESULT FROM count(*)')

I could easily write a php script to do this but I think it can be done pretty easily using mysql queries. 我可以很容易地编写一个php脚本来做到这一点,但是我认为使用mysql查询可以很容易地做到这一点。

Yes, you can combine all of these: 是的,您可以将所有这些结合起来:

INSERT INTO app_stats(appID, day, totalUsers) 
    SELECT 'appID', 'x', count(*)
    FROM users_dailyrecords
     WHERE day = 'x' AND
           appName IN (name FROM app_names);

It is unclear what the appName part is doing. 目前尚不清楚appName部分在做什么。 Presumably, the only appnames in users_dailyrecords should be valid ones in appName . 大概, users_dailyrecords的唯一应用程序名称应该是appName有效名称。 This sounds like a good place for a foreign key relationship. 听起来这是建立外键关系的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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