Solved

MY SQL QUERY For multiple users

Posted on 2013-01-05
3
282 Views
Last Modified: 2013-01-05
Hello Experts,

Stuck with the sql query!
One table contains status and amount and users for a specific proj.
column1,column2,column3,column4
p1,wip,100,user1
p2,wip,100,user2
p3,wip,100,user3

Open in new window

And, the query I tried as
SELECT sum(column3) AS total FROM table1 WHERE column2='wip' and column4='user1' OR column4='user2' OR column4='user3'

Open in new window

This query, returns the result as required. However, if I change column2='unbilled' then also it returns the same results.

I am stuck at this position, as there might be ton's of data and we required to get this column2 and these three users were the fixed users.

Can you suggest the correction in this or any refeerence?

Thanks,
Shail
0
Comment
Question by:ShaileshShinde
3 Comments
 
LVL 15

Accepted Solution

by:
gplana earned 100 total points
ID: 38746708
You should add parentesis:
SELECT sum(column3) AS total FROM table1 WHERE column2='wip' and (column4='user1' OR column4='user2' OR column4='user3')

Open in new window


or even better use IN operator instead of ORs:

SELECT sum(column3) AS total FROM table1 WHERE column2='wip' and column4 IN ('user1','user2','user3')

Open in new window

0
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 38746710
Try with parenthesis or order your OR's and AND's properly, like 2+2*2 is not 8, but 6 :)

Your query is actually:
SELECT sum(column3)
AS total
FROM table1
WHERE ( column2='wip' and column4='user1' )
     OR column4='user2'
     OR column4='user3'
;

Fit your desired results.
0
 
LVL 1

Author Closing Comment

by:ShaileshShinde
ID: 38746748
Thanks A Lot!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now