0

I am running the following SQL query...

$user = 'salesrep_83';
SELECT COUNT(*) AS COUNT
FROM my_table_1 a
JOIN my_table_2 b
ON b.id       = a.sale_id
WHERE a.value = '$user'
AND b.date_posted LIKE '05-05-2014'

This works correctly and shows me all of the sales for the specified $user for todays date. I would like to change this so that it shows me all of the sales for todays date from all sales reps. I can generate and array with all of the users IDs but am unsure how to insert this into the query

Anyone has an example?

2
  • 1
    What if you just remove WHERE a.value = '$user'? Commented May 5, 2014 at 7:16
  • Actually you'd remove a.value = '$user' AND... Commented May 5, 2014 at 7:18

2 Answers 2

2

You can simply use the in operator.

SELECT COUNT(*) AS count 
FROM my_table_1 a JOIN my_table_2 b ON b.id = a.sale_id 
WHERE a.value in ('salesrep_82','salesrep_83')
AND b.date_posted = '05-05-2014'

UPDATED (you're right Rahul):

Using LIKE makes no sense without a wildcard like %

b.date_posted LIKE '05-05-2014'

use

b.date_posted = '05-05-2014'

instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@fightstarr20, condition b.date_posted LIKE '05-05-2014' is actually doing a exact match; in such case why not make it b.date_posted = '05-05-2014'?
0

If you want the aggregate of all users combined, just use the in clause:

SELECT COUNT(*) AS COUNT
FROM my_table_1 a
JOIN my_table_2 b
ON b.id       = a.sale_id
WHERE a.value in ('$user1', '$user2')
AND b.date_posted LIKE '05-05-2014'

If you want to have the list of sales by user, then you can combine the in with group by:

SELECT value, COUNT(*) AS COUNT
FROM my_table_1 a
JOIN my_table_2 b
ON b.id       = a.sale_id
WHERE a.value in ('$user1', '$user2')
AND b.date_posted LIKE '05-05-2014'
GROUP BY value

Note that depending on where you are running this query, it is generally not a good practice to just assign the string value to it because of problems such as SQL injection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.