tags:

views:

48

answers:

2

hey guys

im learning Mysql commands and methods of combine multiple queries , so in order to gain that goal , now i need to combine these queries :

    list($TotalVisitsToday) = $db->sql_fetchrow($db->sql_query("SELECT  COUNT(DISTINCT ip_address) FROM table_iptracking WHERE `date_time` between '$yesterday' and '$today' "));
    list($TotalVisitsYesterday) = $db->sql_fetchrow($db->sql_query("SELECT  COUNT(DISTINCT ip_address) FROM table_iptracking WHERE `date_time` between '$yesterday_1' and '$yesterday'  "));

    list($TotalPVisitsToday) = $db->sql_fetchrow($db->sql_query("SELECT count(ipid) FROM table_iptracking WHERE `date_time` between '$yesterday' and '$today'"));
    list($TotalPVisitsYesterday) = $db->sql_fetchrow($db->sql_query("SELECT count(ipid) FROM table_iptracking WHERE `date_time` between '$yesterday_1' and '$yesterday'"));

    list($TotalCrawlers) = $db->sql_fetchrow($db->sql_query("SELECT count(ipid) FROM table_iptracking WHERE  hostname LIKE '%crawl%' "));

i have no clue on how to mix the above lines !

A: 

You could form the SQL statement into a UNION, something like...

SELECT COUNT(DISTINCT ip_address)
    FROM table_iptracking
    WHERE `date_time` between '$yesterday' and '$today'
UNION
SELECT COUNT(DISTINCT ip_address)
    FROM table_iptracking
    WHERE `date_time` between '$yesterday_1' and '$yesterday'
UNION
SELECT count(ipid)
    FROM table_iptracking
    WHERE `date_time` between '$yesterday' and '$today'
UNION
SELECT count(ipid)
    FROM table_iptracking
    WHERE `date_time` between '$yesterday_1' and '$yesterday'
UNION
SELECT count(ipid)
    FROM table_iptracking
    WHERE hostname LIKE '%crawl%'

I can't say that I would recommend this, though. A result set where different rows mean different things is likely to prove troublesome in the long run.

Brian Hooper
i tried UNION before you suggested this answer but the result is ZERO and not works
Mac Taylor
A: 

I'm not sure about your final goal, but if you just want it in one query, you can do it like this:

SELECT
(SELECT COUNT(DISTINCT ip_address) FROM table_iptracking WHERE 'date_time' between '$yesterday' and '$today') COUNT1,
(SELECT COUNT(DISTINCT ip_address) FROM table_iptracking WHERE 'date_time' between '$yesterday_1' and     '$yesterday') COUNT2,
(SELECT COUNT(ipid) FROM table_iptracking WHERE 'date_time' between '$yesterday' and '$today') COUNT3,
(SELECT COUNT(ipid) FROM table_iptracking WHERE 'date_time' between '$yesterday_1' and '$yesterday') COUNT4,
(SELECT COUNT(ipid) FROM table_iptracking WHERE  hostname LIKE '%crawl%') COUNT5

(you can omit the FROM caluse in MySQL)

Waleed Eissa
your answer was cool and i tried it , but it dose not work , are you sure about the syntax ?
Mac Taylor
Yes, the syntax is correct, what error do you get?
Waleed Eissa
Also, is it MySQL or PHP related? Are you sure the variable placeholders are replaced correctly with values in your PHP script? Unfortunately I can't help you with PHP as I'm an ASP.NET developer, but the syntax is certainly valid for MySQL (and quite simple actually). Try to run the query from a MySQL client (like the MySQL command line) to be sure that it works correctly.
Waleed Eissa