Solved

Mysql Query - shorter way to see if row contains a number

Posted on 2010-08-14
5
385 Views
Last Modified: 2013-12-13
Dear experts,

I have a query that I think I am making more complicated than it needs to be and would like to make it simpler.

I have attached the query below, what I have is this statement that tell me to show the record if it contains the number 1,2,3 if not to show an empty record "orderdetails.DetailItemID = 1 OR orderdetails.DetailItemID = 2 OR orderdetails.DetailItemID = 3".

Is there an easier way to say this i.e ( i know this does not work but something like) "orderdetails.DetailItemID = 1,2,3" I do not want to use <= 3 because in the future I may need to include more numbers that are not in order i.e 1,2,3,7,10 that the query will need to check for.

Many thanks as always for your help

SELECT orderdetails.DetailOrderID, 
	orderdetails.DetailItemID
FROM orderdetails
WHERE orderdetails.DetailsMemberID = 1 AND orderdetails.ProgramActivated = 0 AND  orderdetails.DetailItemID = 1 OR orderdetails.DetailItemID = 2 OR orderdetails.DetailItemID = 3 
ORDER BY orderdetails.DetailsID DESC
LIMIT 1

Open in new window

0
Comment
Question by:Lightwalker
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Tekati68
ID: 33439294
You can use BETWEEN as follows.
SELECT orderdetails.DetailOrderID, 

	orderdetails.DetailItemID

FROM orderdetails

WHERE orderdetails.DetailsMemberID = 1 AND orderdetails.ProgramActivated = 0 AND orderdetails.DetailItemID BETWEEN 1 AND 3 

ORDER BY orderdetails.DetailsID DESC

LIMIT 1

Open in new window

0
 
LVL 2

Expert Comment

by:Tekati68
ID: 33439299
Also note that they way you have it written above would give you what you want PLUS it would give you orderdetails.DetailItemID's 2 and 3 regardless of what orderdetails.DetailsMemberID and orderdetails.ProgramActivated were.  You need to enclose it in open and closed () in order to do what you were thinking like below.
SELECT orderdetails.DetailOrderID, 

	orderdetails.DetailItemID

FROM orderdetails

WHERE orderdetails.DetailsMemberID = 1 AND orderdetails.ProgramActivated = 0 AND (orderdetails.DetailItemID = 1 OR orderdetails.DetailItemID = 2 OR orderdetails.DetailItemID = 3)

ORDER BY orderdetails.DetailsID DESC

LIMIT 1

Open in new window

0
 

Author Comment

by:Lightwalker
ID: 33439304
Thanks Tekati68,

In the future on the same query I will have to run this query to check numbers that are out of sequence i.e 1,2,3,7,10 so the between clause will not work for this. in there another more efficient way to check for numbers out of sequence.

thanks for your help
0
 
LVL 2

Accepted Solution

by:
Tekati68 earned 125 total points
ID: 33439310
Ah yes in that case you would want to use the IN keyword as in...
SELECT orderdetails.DetailOrderID, 

	orderdetails.DetailItemID

FROM orderdetails

WHERE orderdetails.DetailsMemberID = 1 AND orderdetails.ProgramActivated = 0 AND orderdetails.DetailItemID IN (1,2,3,7,10) 

ORDER BY orderdetails.DetailsID DESC

LIMIT 1

Open in new window

0
 

Author Comment

by:Lightwalker
ID: 33439316
Thank you Tekati68, that's perfect I'm really grateful
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculating number of days between two dates 7 32
php string masking 4 29
Preventing users manipulating URL strings 10 35
Session timeout 5 14
Creating and Managing Databases with phpMyAdmin in cPanel.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

920 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

13 Experts available now in Live!

Get 1:1 Help Now