Solved

php mysql select question

Posted on 2014-11-13
6
289 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

9 Experts available now in Live!

Get 1:1 Help Now