Solved

php mysql select question

Posted on 2014-11-13
6
305 Views
Last Modified: 2014-11-16
Hello,

I have:
$yfrom = 1970;
$yto = 1974;

Current select snippet:
listing_title LIKE '%".$yto."%' <- this finds all of the listings with the year in the title

and I would like it to find a range, is there a way to find a range of numbers? I am guessing that I need to break it out like

listing_title LIKE '%1970%' OR listing_title LIKE '%1971%' OR listing_title LIKE '%1972%' OR listing_title LIKE '%1973%'

is there a dynamic way to do this?

Thanks,
Matt
0
Comment
Question by:movieprodw
6 Comments
 
LVL 8

Expert Comment

by:vr6r
ID: 40440547
Are the years in the titles always in the same place?
If so, you could try extracting them with a string function (eg: substring() if they're always in a certain spot in the middle of the title, or right(4) if they're always at the end, etc) and then compare against that value or try a BETWEEN() clause on that result for a range of years.
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 125 total points
ID: 40440555
You could use BETWEEN or >= and <=.
0
 
LVL 11

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 125 total points
ID: 40440996
BETWEEN will work if column has some countable data type like int or date/timestamp

For strings only LIKE might be of use or the way author suggested himself.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Accepted Solution

by:
vr6r earned 125 total points
ID: 40441051
BETWEEN will evaluate strings.  

select case when right("abc1964", 4) BETWEEN "1950" AND "1970" THEN "true" else "false" end
-----
result = "true"

select case when right("abc1964", 4) BETWEEN "1950" AND "1960" THEN "true" else "false" end
-----
result = "false"

Open in new window


The real question is if the date portion of the string can be reliably extracted from the title.  If it's in a different position within the title for each record then it would be difficult.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 40443733
Can you please show us the CREATE TABLE statement and some of your test data?
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40446634
Thank you for the responses, it looks like I have to rebuild how the data is stored in order to properly achieve this.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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