drktvjp713333 2013-08-28 19:38 采纳率: 0%
浏览 44
已采纳

mysql中的临时表问题

I am working on a little reporting and think I need to use temp tables. My code performs a api call to get all the user cookies that interacting with that system for the day.

Now I have a table with all the cookies and the type of browser they use, I could just loop through the array of cookies provided by the api and do a sql look up for each and incriment counters for the diff browsers, so in the end I can report the number of browser access's done by different browsers.

This however as you can imagine could take very long if there are allot of cookies (which there are).

From what I know the solution would be to make a temp table of the cookies passed to me and innerjoin it to my cookie data table and then do a

select count(id),broswer ... group by browser

Below is a more code view of what I want to try and do:

$json = file_get_content($apiURL);
$cookies = '';
foreach($json as $val){$cookies .= ','.$val['cookie']; }
$cookies = substr($cookies,1);

mysql_query('CREATE TEMPORARY TABLE passedcookies (
  `cookie` varchar(200) NOT NULL,
  PRIMARY KEY(cookie)
) values ($cookies);')

mysql_query('SELECT count(cd.id), cd.browser FROM cookiedata cd
            INNER JOIN passedcookies pc
            ON pc.cookie = cd.cookie
            GROUP BY cd.browser;');

mysql_query('DESTROY TEMP TBL;');

Do I have to destroy the temp table? when is it destroyed? and if 3 users run the report will each get their own temp table?

Thanks for any help

  • 写回答

1条回答 默认 最新

  • duanmei1922 2013-08-28 19:50
    关注

    You can destroy the temp table if you wish. If you don't, it will be destroyed automatically when your MySQL connection is closed. Unless you're using shared connections, this will happen when the PHP script exits.

    If you're not using shared connections, then each user will get their own temp tables.

    P.S. Your SQL syntax for creating the temporary table is wrong, but you didn't ask about that. There's no VALUES clause in CREATE TEMPORARY TABLE, that belongs in an INSERT statement that you use after it.

    Actually, for what you're doing, I don't think you need a temporary table. The query:

    SELECT browser, count(*)
    FROM cookiedata
    WHERE cookie in ($cookies)
    GROUP BY browser
    

    should work fine. I think MySQL implements IN (list) pretty well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?