I need to SUM the contents of a column which is already worked out using GROUP BYs.. How exactly would you go about that?
The group should be based on the user name, not the entire contents of the result set. I believe this essentially a group by on that username field, but that i believe would break how the query currently works..
Example below:
SELECT A1.USERNAME, DATE_FORMAT(FROM_UNIXTIME(A1.TIME_STAMP),'%Y-%m-%d') AS DTTM, A1.ACCTSESSIONID,
MAX(IFNULL(A1.ACCTINPUTGW,0) * POW(2,32) + IFNULL(A1.ACCTINPUTOCT, 0)) - MAX(IFNULL(A2.ACCTINPUTGW,0) * POW(2,32) + IFNULL(A2.ACCTINPUTOCT, 0)) as TOTAL_UPLOAD,
MAX(IFNULL(A1.ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(A1.ACCTOUTPUTOCT, 0)) - MAX(IFNULL(A2.ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(A2.ACCTOUTPUTOCT, 0)) as TOTAL_DOWNLOAD
FROM ACCOUNTING A1
LEFT JOIN ACCOUNTING A2
ON A1.ACCTSESSIONID = A2.ACCTSESSIONID
AND DATE_FORMAT(FROM_UNIXTIME(A2.TIME_STAMP), '%Y-%m-%d') = '2011-07-04'
WHERE DATE_FORMAT(FROM_UNIXTIME(A1.TIME_STAMP), '%Y-%m-%d') = '2011-07-05'
GROUP BY A1.ACCTSESSIONID,A2.ACCTSESSIONID
ORDER BY A1.USERNAME
Edit:
The columns would be: TOTAL_DOWNLOAD and TOTAL_UPLOAD
Thanks # ypercube, worked a treat
SELECT A3.USERNAME
, SUM(A3.TOTAL_UPLOAD) AS FINAL_UPLOAD
, SUM(A3.TOTAL_DOWNLOAD) AS FINAL_DOWNLOAD
FROM
( SELECT
A1.USERNAME
, DATE_FORMAT(FROM_UNIXTIME(A1.TIME_STAMP),'%Y-%m-%d') AS DTTM
, A1.ACCTSESSIONID
, MAX(IFNULL(A1.ACCTINPUTGW,0) * POW(2,32) + IFNULL(A1.ACCTINPUTOCT, 0))
- MAX(IFNULL(A2.ACCTINPUTGW,0) * POW(2,32) + IFNULL(A2.ACCTINPUTOCT, 0))
AS TOTAL_UPLOAD
, MAX(IFNULL(A1.ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(A1.ACCTOUTPUTOCT, 0))
- MAX(IFNULL(A2.ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(A2.ACCTOUTPUTOCT, 0))
AS TOTAL_DOWNLOAD
FROM ACCOUNTING A1
LEFT JOIN ACCOUNTING A2
ON A1.ACCTSESSIONID = A2.ACCTSESSIONID
AND DATE_FORMAT(FROM_UNIXTIME(A2.TIME_STAMP), '%Y-%m-%d') = '2011-07-04'
WHERE DATE_FORMAT(FROM_UNIXTIME(A1.TIME_STAMP), '%Y-%m-%d') = '2011-07-05'
GROUP BY A1.ACCTSESSIONID,A2.ACCTSESSIONID
ORDER BY A1.USERNAME
) AS A3
GROUP BY A3.USERNAME
Related
I have quite the beast of a SQL statement and I was hoping for a little help. I have found that I need to be using an OUTER join for some of these tables but MySQL doesn't have that function.
SELECT validclick.CampaignName AS CampaignName,
validclick.Website AS Website,
validclick.RevClicks AS RevClicks,
validclick.Revenue AS Revenue,
validclick.TQ AS TQ,
validclick.Source AS Source,
validclick.ScoredClicks AS ScoredClicks,
validclick.Searches AS Searches,
vci.Impressions AS Impressions,
(validclick.RevClicks / validclick.Searches * 100) AS CTR,
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0)) AS Spend,
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0)) AS PaidClicks,
(validclick.Revenue -
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0))) AS Profit,
(validclick.TQ / validclick.ScoredClicks) AS ScoredTQ,
(validclick.TQ / validclick.RevClicks) AS UnscoredTQ,
(validclick.Revenue /
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0))) AS RPI,
(validclick.Revenue / validclick.RevClicks) AS RPC,
((IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0)) /
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0))) AS CPC,
((validclick.Revenue -
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0))) /
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0)) * 100) AS ROI,
(validclick.RevClicks /
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0)) * 100) AS Conversion
FROM
(SELECT camp.CampaignName AS CampaignName,
MAX(camp.Website) AS Website,
MAX(camp.Source) AS Source,
SUM(vc.Clicks) AS RevClicks,
SUM(vc.Revenue) AS Revenue,
SUM(vc.TQ) AS TQ,
SUM(vc.ScoredClicks) AS ScoredClicks,
SUM(vc.BiddedSearches) AS Searches
FROM
(SELECT AffID,
MAX(CampaignName) AS CampaignName,
Website,
MAX(Source) AS Source
FROM campaigns
GROUP BY AffID) AS camp
JOIN
(SELECT AffID,
SUM(Clicks) AS Clicks,
SUM(AffiliateRevenue) AS Revenue,
SUM(BiddedSearches) AS BiddedSearches,
SUM(CASE WHEN TQ > 0 THEN (TQ * Clicks) ELSE NULL END) AS TQ,
SUM(CASE WHEN TQ > 0 THEN Clicks ELSE NULL END) AS ScoredClicks
FROM validclickvc
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY AffID) AS vc
ON vc.AffID = camp.AffID
GROUP BY camp.CampaignName) AS validclick
LEFT JOIN
(SELECT CampaignName,
SUM(Spend) AS Spend,
SUM(OutboundClicks) AS PaidClicks
FROM facebookads
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY CampaignName) AS facebook
ON validclick.CampaignName = facebook.CampaignName
LEFT JOIN
(SELECT CampaignName,
SUM(Spend) AS Spend,
SUM(Clicks) AS PaidClicks
FROM yahoo
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY CampaignName) AS yaho
ON validclick.CampaignName = yaho.CampaignName
LEFT JOIN
(SELECT CampaignName,
SUM(Cost) AS Spend,
SUM(Clicks) AS PaidClicks
FROM adwords
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY CampaignName) AS google
ON validclick.CampaignName = google.CampaignName
LEFT JOIN
(SELECT camp.CampaignName AS CampaignName,
SUM(vc.Impressions) AS Impressions
FROM
(SELECT AffID,
MAX(CampaignName) AS CampaignName
FROM campaigns
GROUP BY AffID) AS camp
JOIN
(SELECT ty AS AffID,
COUNT(DISTINCT(id)) AS Impressions
FROM validclickimpressions
WHERE ts BETWEEN '2018-05-06 00:00' AND '2018-05-07 23:59'
GROUP BY AffID) AS vc
ON vc.AffID = camp.AffID
GROUP BY camp.CampaignName) AS vci
ON validclick.CampaignName = vci.CampaignName
ORDER BY CampaignName;
I'm aware this may not be the most efficient MySQL statement, but we will soon be changing the structure of things so i's not a big deal. For now I need some way to use OUTER join, or do a LEFT JOIN UNTION RIGHT JOIN but with an alias. The tables (aliases) I need to full join are vci, facebook, google, and yaho. Any ideas on how to tackle this?
MySQL does not support FULL [OUTER] JOIN.
We can emulate a full outer join in MySQL using two separate select, combining the results. The pattern of an outer join and an anti-join.
-- outer join (all rows from a along with matching rows from b)
SELECT ... FROM a LEFT JOIN b ON ...
UNION ALL
-- anti-join (rows from b with no matching row in a)
SELECT ... FROM b LEFT JOIN a ON ... WHERE a.notnull_col IS NULL
As a simplistic demonstration, two tables a and b:
table a table b
an bn
------- -------
1 2
2 3
3 5
4 7
Example query
-- left outer join (rows in a with matching rows from b)
SELECT a.an, b.bn
FROM a
LEFT
JOIN b
ON b.bn = a.an
UNION ALL
-- anti-join (rows from b with no match in a)
SELECT a.an, b.bn
FROM b
LEFT
JOIN a
ON a.an = b.bn
WHERE a.an IS NULL
should return something like this (without ORDER BY clause, order of rows is indeterminate)
an bn
------- -------
1 (NULL)
2 2
3 3
4 (NULL)
(NULL) 5
(NULL) 7
I have a MySQL table with the following structure and data:
Increments
id emp_id starting_salary increment_rate increment_frequency
2 340 5000 250 1
3 340 5000 250 4
I need to have aliases, a and b which will hold some value based on the following formula:
starting_salary + (increment_rate * increment_frequency)
To be precise, I want a = 5250 (based on a = (5000 + (250 * 1))) and b = 6000 (based on b = (5000 + (250 * 4)))
Now I have another table with the following data:
PaySlips
id employee_id salary_month arrear
173824 340 '2015-06-01' 2350
I want to join a and b that I got from the table Increments with table PaySlips. And I want to use a and b in the following way:
((a * 8) / 30 + (b * 22) / 30)
My alias will be basic_salary. So basic_salary will hold this value from the above calculation:
basic_salary = ((a * 8) / 30 + (b * 22) / 30)
= ((5250 * 8) / 30 + (6000 *22) / 30)
= (1400 + 4400)
= 5800
I've got no idea how to do this. Can anyone please help me?
All I got are the common columns in both tables - emp_id and employee_id and I can join both tables using these columns. I just can't figure out how I can store the above values and organize the calculation inside my query.
Sample query:
SELECT x.id, x.employee_id,
(*my calculation using a and b from table Increments*) AS basic_salary,
x.salary_month, x.arrear
FROM PaySlips x
JOIN Increments y
ON x.employee_id = y.emp_id
For determining a:
SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS a
FROM Increments
WHERE id = 2
And for determining b:
SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS b
FROM Increments
WHERE id = 3
MySQL version: 5.2
I'm not clear on all the details, for example what should happen if there are three rows for one employee in increments? Anyhow, here's a sketch to start with:
select employee_id
, ((a * 8) / 30 + (b * 22) / 30) as basic_salary
from (
select x.employee_id
, min(starting_salary + (increment_rate * increment_frequency)) as a
, max(starting_salary + (increment_rate * increment_frequency)) as b
, x.salary_month, x.arrear
from payslips x
join increments y
on x.employee_id = y.emp_id
group by x.employee_id, x.salary_month, x.arrear
) as t
If id 2 and 3 are the criteria (I assumed they are examples) you can use a case statement like:
select employee_id
, ((a * 8) / 30 + (b * 22) / 30) as basic_salary
from (
select x.employee_id
, max(starting_salary + (increment_rate * case when y.id = 2 then increment_frequency end )) as a
, max(starting_salary + (increment_rate * case when y.id = 3 then increment_frequency end)) as b
, x.salary_month
, x.arrear
from payslips x
join increments y
on x.employee_id = y.emp_id
group by x.employee_id, x.salary_month, x.arrear
) as t;
In this case it does not matter what aggregate you use, it is to get rid of the row that contains null.
based on the requirements you added i think something like this will solve your problems:
SELECT PS.id, PS.employee_id, ((A.value * 8) / 30 + (B.value * 22) / 30) AS basic_salary
FROM PaySlips AS PS
JOIN (
SELECT I.emp_id, I.starting_salary + (increment_rate * increment_frequency) AS VALUE
FROM Increments AS I
WHERE I.id = 2
) AS A
ON A.emp_id = PS.employee_id
JOIN (
SELECT I.emp_id, I.starting_salary + (increment_rate * increment_frequency) AS value
FROM Increments AS I
WHERE I.id = 3
) AS B
ON B.emp_id = PS.employee_id
This version might need some alteration if it's not working on your real scenario, but please feel free to tell if anything else needs amending.
Hope it helps.
For determining and setting #a variable:
SET #a := (SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS a
FROM Increments
WHERE id = 2);
And for determining and setting #b variable:
SET #b := (SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS b
FROM Increments
WHERE id = 3);
Then you can use #a and #b in your main query;
you can test simply by
SELECT #a as a;
SELECT #b as b;
SELECT
x.id,
x.employee_id,
(y.a * 8) / 30 + (y.b * 22) / 30 as basic_salary,
x.salary_month,
x.arrear
FROM PaySlips x
JOIN (
select t1.emp_id, t1.a, t2.b
from (
select
emp_id,
starting_salary + increment_rate * increment_frequency as a
from Increments
where id = 2
) as t1
join (
select
emp_id,
starting_salary + increment_rate * increment_frequency as b
from Increments
where id = 3
) as t2
on t1.emp_id = t2.emp_id
) as y
ON x.employee_id = y.emp_id
I have one problem with this query; I can't seem to get ((total + rec_host) / 2) AS total2 to work. How would I go about this procedure without doing:
((((rank_ur + rank_scs + rank_tsk + rank_csb + rank_vfm + rank_orr) / 6) + rec_host ) / 2)
Here's my Query:
SELECT host_name,
SUM(rank_ur) AS cnt1,
SUM(rank_scs) AS cnt2,
SUM(rank_tsk) AS cnt3,
SUM(rank_csb) AS cnt4,
SUM(rank_vfm) AS cnt5,
SUM(rank_orr) AS cnt6,
SUM(IF(rec_host = 1,1,0)) AS rh1,
SUM(IF(rec_host = 0,1,0)) AS rh2,
((rank_ur + rank_scs + rank_tsk + rank_csb + rank_vfm + rank_orr) / 6) AS total,
((total + rec_host) / 2) AS total2
FROM lhr_reviews
GROUP BY host_name
ORDER BY total
DESC LIMIT 0,10
Use a subquery like so:
SELECT
host_name,
cnt1,
cnt2,
cnt3,
cnt4,
cnt5,
cnt6,
rh1,
rh2,
total,
((total + rec_host) / 2) AS total2
FROM
(
SELECT host_name,
rec_host,
SUM(rank_ur) AS cnt1,
SUM(rank_scs) AS cnt2,
SUM(rank_tsk) AS cnt3,
SUM(rank_csb) AS cnt4,
SUM(rank_vfm) AS cnt5,
SUM(rank_orr) AS cnt6,
SUM(IF(rec_host = 1,1,0)) AS rh1,
SUM(IF(rec_host = 0,1,0)) AS rh2,
((rank_ur + rank_scs + rank_tsk +
rank_csb + rank_vfm + rank_orr
) / 6) AS total
FROM lhr_reviews
GROUP BY host_name, rec_host
) t
ORDER BY total
DESC LIMIT 0,10;
What you could do is this:
select x.*, ((x.total + rec_host) / 2) AS total2
from (
SELECT host_name, rec_host,
SUM(rank_ur) AS cnt1,
SUM(rank_scs) AS cnt2,
SUM(rank_tsk) AS cnt3,
SUM(rank_csb) AS cnt4,
SUM(rank_vfm) AS cnt5,
SUM(rank_orr) AS cnt6,
SUM(IF(rec_host = 1,1,0)) AS rh1,
SUM(IF(rec_host = 0,1,0)) AS rh2,
((rank_ur + rank_scs + rank_tsk + rank_csb + rank_vfm + rank_orr) / 6) AS total
FROM lhr_reviews
GROUP BY host_name
ORDER BY total
DESC LIMIT 0,10
) as x
;
You cannot use the column as an alias when the alias and other column are in the same level of SELECT. So you can use a derived query which lets you basically rename your columns and/or name any computed columns.Check on Rubens Farias and Rob Van Dam answer here
PS: will search for a better article to update the answer :)
I have two tables.
a) ai_account
b) ai_order_product
i want to do some calculations for particular supplier_id.
1, totalAmount, i want to do something like
SUM(ai_order_product.quantity * ai_order_product.cost)
2, amountPaid, this is total amount paid by the supplier that will be
something like SUM(ai_account.amount) with reference to supplier_id.
3) balance, this will be calculated by SUM(ai_order_product.quantity *
ai_order_product.cost) - SUM(ai_invoice.amount)
4) lastPayment date, that will be MAX(ai_account.addDate).
i tried doing something like this.
SELECT SUM(op.quantity * op.cost) as totalAmount,
SUM(ac.amount) as amountPaid,
SUM(op.quantity * op.cost) - SUM(ac.amount) as balance,
MAX(ac.addDate) as lastPayment
FROM ai_order_product op
LEFT JOIN ai_account ac
ON (op.supplier_id = ac.trader_id)
WHERE op.supplier_id = 42
it does not work properly, it fetches some unexpected values, whereas the result for the above being expected is,
for supplier_id = 42,
1) totalAmount = 1375,
2) amountPaid = 7000,
3) balance = -5625,
4) lastPayment = 2011-11-23
and for supplier_id = 35,
1) totalAmount = 1500,
2) amountPaid = 43221,
3) balance = -41721,
4) lastPayment = 2011-11-28
and for supplier_id = 41
1) totalAmount = 0
2) amountPaid = 3000,
3) balance = -3000,
4) lastPayment = 2011-11-09
i want to fetch one row by supplier_id.
P.S: i just entered some dummy values that is why the calculations are mostly negative whereas in application the calculated values will be positive.
Thats because each "ai_order_product" row is getting counted multiple times (once for each row present in the ai_account table).
Try this:
SELECT
op.totalAmount as totalAmount
, SUM(ac.amount) as amountPaid
, op.totalAmount - SUM(ac.amount) as balance
, MAX(ac.addDate) as lastPayment
FROM (
select supplier_id, sum(quantity * cost) as totalAmount
from ai_order_product
group by supplier_id) op
LEFT JOIN ai_account ac ON (op.supplier_id = ac.trader_id)
WHERE op.supplier_id = 42
This may be slightly off, but this general logic should work.
You have to use to state GROUP BY, when using aggregate functions like SUM in SELECT statements.
SELECT op.supplier_id as supplierId,
SUM(op.quantity * op.cost) as totalAmount,
SUM(ac.amount) as amountPaid,
SUM(op.quantity * op.cost) - SUM(ac.amount) as balance,
MAX(ac.addDate) as lastPayment
FROM ai_order_product op
LEFT JOIN ai_account ac
ON (op.supplier_id = ac.trader_id)
GROUP BY op.supplier_id
HAVING supplierId = 42
SELECT
SUM(op.quantity * op.cost) as totalAmount
, ac2.amountPaid
, SUM(op.quantity * op.cost) - ac2.balance
, ac2.lastPayment
FROM ai_order_product op
LEFT JOIN (SELECT
ac.supplier_id
, MAX(ac.addDate) as lastPayment
, SUM(ac.amount) as balance
FROM ai_account ac
WHERE (op.supplier_id = ac.supplier_id)
GROUP BY ac.supplier_id) ac2 ON (ac2.supplier_id = op.supplier_id)
WHERE op.supplier_id = 42
GROUP BY op.supplier_id
The group by clauses kick in when you're selecting more than one supplier_id.
Notice that your expected and actual values are doubled. After entering your sample data and running the query, I get (for supplier_id = 42)
+-------------+------------+---------+-------------+
| totalAmount | amountPaid | balance | lastPayment |
+-------------+------------+---------+-------------+
| 2750 | 14000 | -11250 | 2011-11-23 |
+-------------+------------+---------+-------------+
That's because you've got 2 rows in each table that match the join criteria, causing a doubling of the results.
I think you need to first get the sub totals for one of the tables so that you only have 1 row coming back. thus the inner select here returns the max and the sum, so when you join to it you only get 1 row.
SELECT SUM(op.quantity * op.cost) as totalAmount,
ac.addDate as lastPayment,
ac.amount as amountPaid,
SUM(op.quantity * op.cost) - SUM(ac.amount) as balance
FROM ai_order_product op
INNER JOIN (
SELECT max(IaiaddDate) as addDate, sum(iai.amount) as Amount, iai.supplier_ID
FROM ai_account iai
Group by supplier_ID) ac
ON AC.Supplier_ID = Op.Supplier_ID
WHERE op.supplier_id = 42
Group by Ac.addDate, ac.amount, op.supplier_ID --Just incase where clause is left off.
The following MySQL query produces me a list of session_ids and associated usage. What i would like to do is group each session into one row with the greatest upload and download displaying. There can be multiple repeats of a user name, it has to be grouped on the session.
When I try and use group by, the greatest is not always selected.
SELECT USERNAME, ACCTSESSIONID,
IFNULL(ACCTINPUTGW ,0) * POW(2,32) + IFNULL(ACCTINPUTOCT , 0) as TOTAL_UPLOAD,
IFNULL(ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(ACCTOUTPUTOCT, 0) as TOTAL_DOWNLOAD
FROM ACCOUNTING
WHERE DATE_FORMAT(FROM_UNIXTIME(TIME_STAMP), '%Y-%m-%d') = '2011-07-05'
ORDER BY USERNAME ASC, ACCTSESSIONID
-
USERNAME ACCTSESSIONID TOTAL_UPLOAD TOTAL_DOWNLOAD
kor1 SESSION232442 341594114 5671726599
kor1 SESSION232442 331306202 5571382940
kor1 SESSION232444 338083784 5609510490
kor1 SESSION454355 323367019 5451121083
kor2 SESSION943209 323132957 5450522047
ran32 SESSION934082 323132957 5450522047
ran62 SESSIONA34324 9532356 5450523537
You should use MIN()/MAX() aggregate functions for this:
SELECT USERNAME, ACCTSESSIONID,
MAX(IFNULL(ACCTINPUTGW ,0) * POW(2,32) + IFNULL(ACCTINPUTOCT , 0)) as TOTAL_UPLOAD,
MAX(IFNULL(ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(ACCTOUTPUTOCT, 0)) as TOTAL_DOWNLOAD
FROM ACCOUNTING
WHERE
DATE_FORMAT(FROM_UNIXTIME(TIME_STAMP), '%Y-%m-%d') = '2011-07-05'
GROUP BY
USERNAME, ACCTSESSIONID
ORDER BY
USERNAME ASC, ACCTSESSIONID
More about aggregate function in MySQL: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT USERNAME, ACCTSESSIONID,
MAX(IFNULL(ACCTINPUTGW ,0) * POW(2,32) + IFNULL(ACCTINPUTOCT , 0))
AS TOTAL_UPLOAD,
MAX(IFNULL(ACCTOUTPUTGW,0) * POW(2,32) + IFNULL(ACCTOUTPUTOCT, 0))
AS TOTAL_DOWNLOAD
FROM ACCOUNTING
WHERE DATE_FORMAT(FROM_UNIXTIME(TIME_STAMP), '%Y-%m-%d') = '2011-07-05'
GROUP BY USERNAME ASC, ACCTSESSIONID
ORDER BY USERNAME ASC, ACCTSESSIONID
Note 1: Instead of IFNULL(), you can also use COALESCE(). It may be preferable as it can have more than 2 arguments and it's also used in many other RDBMSs.
Note 2: Instead of:
DATE_FORMAT(FROM_UNIXTIME(TIME_STAMP), '%Y-%m-%d') = '2011-07-05'
you can use:
TIME_STAMP >= '2011-07-05' AND TIME_STAMP < '2011-07-06'
No need to call 2 functions for every row in the table.