Solved

php mysql select question

Posted on 2014-11-13
6
315 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

840 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