Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

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

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
Lightwalker
Asked:
Lightwalker
  • 3
  • 2
1 Solution
 
Tekati68Commented:
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
 
Tekati68Commented:
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
 
LightwalkerAuthor Commented:
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
 
Tekati68Commented:
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
 
LightwalkerAuthor Commented:
Thank you Tekati68, that's perfect I'm really grateful
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now