473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can MYSQL do this...?

I am very new to mysql so please forgive me if I may be asking
something obvious - I'm also not likely to fully understand the answer
but if I know it can be done and someone could point me in the right
direction I will spend the time to learn what I need to get things
going.

If I create a database can I do the following easily using MYSQL (with
PHP or similair)?

I want to set up a system where a user sets sliders to different
positions to select things. Each item will have a value for each
sliders stored with it and then as the user changes the sliders I want
to be able to check the database to find the closest match to the
users sliders.

In a bit more detail, I have six sliders, each with a value between
one and 20, I want my system to compare the users settings to each
entry in the database and find the least different. Each item would be
scored - so if the user puts in 10 on a slider but the database entry
is 8 or 12 then 2 is added to the score of that item. The lower the
score the closer the match. If all the sliders are the same for an
item as those the user enters the score will be 0. Is there a quick
way for MYSQL to do this or is it a matter of doing the maths in the
application rather than via a simple query?

I hope this makes sense. I'm looking to find the quickest and simplest
way to do search like this through a database of about 5000 items.

Cheers

Stew Dean
Jul 19 '05 #1
4 3841
stew dean wrote:
I am very new to mysql so please forgive me if I may be asking
something obvious - I'm also not likely to fully understand the answer
but if I know it can be done and someone could point me in the right
direction I will spend the time to learn what I need to get things
going.

If I create a database can I do the following easily using MYSQL (with
PHP or similair)?

I want to set up a system where a user sets sliders to different
positions to select things. Each item will have a value for each
sliders stored with it and then as the user changes the sliders I want
to be able to check the database to find the closest match to the
users sliders.

In a bit more detail, I have six sliders, each with a value between
one and 20, I want my system to compare the users settings to each
entry in the database and find the least different. Each item would be
scored - so if the user puts in 10 on a slider but the database entry
is 8 or 12 then 2 is added to the score of that item. The lower the
score the closer the match. If all the sliders are the same for an
item as those the user enters the score will be 0. Is there a quick
way for MYSQL to do this or is it a matter of doing the maths in the
application rather than via a simple query?

I hope this makes sense. I'm looking to find the quickest and simplest
way to do search like this through a database of about 5000 items.


So if I understood you correctly.. You would have table like this:

| item |
--------
|id
|slider1
|slider2
|slider3
|slider4
|slider5
|slider6

And you would have 5000 rows in that table. Then user gives you 6 values
that represent the values of slider1-6. Then you will calculate the
difference value for each item and return closest matches.

Difference value will be calculated like this:
If item in database has values:
1,10,5,10,10,10

And user enters values: 1,5,10,15,10,10

You would get difference:
abs(1-1) + abs(5-10) + abs(10-5) + abs(15-10) + abs(10-10) + abs(10-10)
= abs(0) + abs(-5) + abs(5) + abs(10) + abs(0) + abs(0)
= 0 + 5 + 5 + 10 + 0 + 0
= 20

If this is the case, then let's have a look at the example I created:
#################

create table item(id int, value1 int,value2 int,value3 int,value4
int,value5 int, value6 int);

insert into item values(1,1,1,1,1,1,1);
insert into item values(2,10,10,10,10,10,10);
insert into item values(3,10,10,10,10,10,10);
insert into item values(4,11,10,10,10,10,10);

select * from item;
+------+--------+--------+--------+--------+--------+--------+
| id | value1 | value2 | value3 | value4 | value5 | value6 |
+------+--------+--------+--------+--------+--------+--------+
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 10 | 10 | 10 | 10 | 10 | 10 |
| 3 | 10 | 10 | 10 | 10 | 10 | 10 |
| 4 | 11 | 10 | 10 | 10 | 10 | 10 |
+------+--------+--------+--------+--------+--------+--------+
4 rows in set (0.00 sec)

# So if you search match for these values( 1,1,3,1,1,1 ):

select id,
(abs(value1-1)+abs(value2-1)+abs(value3-3)+abs(value4-1)+abs(value5-1)+abs(value6-1))
as value from item order by value limit 1;

+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
+------+-------+

Please note, that it is possible that several items can have same value,
and this only returns first one. ( You asked only for one. )

Without "limit" the query would return them all, like this:
+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
| 2 | 52 |
| 3 | 52 |
| 4 | 53 |
+------+-------+

However, what comes to speed issues. The speed of this query will
increase as the amount of rows increase in the database, since query
will have to do calculations for each row every time it is executed.
5000 rows? I don't think you will get problems with speed, but better
try it out to be sure.

I am not an expert what comes to mathematics, so I'm not sure if there
is a better way to calculate the difference value. Searching for exact
match would be faster.

Jul 19 '05 #2
stew dean wrote:
I am very new to mysql so please forgive me if I may be asking
something obvious - I'm also not likely to fully understand the answer
but if I know it can be done and someone could point me in the right
direction I will spend the time to learn what I need to get things
going.

If I create a database can I do the following easily using MYSQL (with
PHP or similair)?

I want to set up a system where a user sets sliders to different
positions to select things. Each item will have a value for each
sliders stored with it and then as the user changes the sliders I want
to be able to check the database to find the closest match to the
users sliders.

In a bit more detail, I have six sliders, each with a value between
one and 20, I want my system to compare the users settings to each
entry in the database and find the least different. Each item would be
scored - so if the user puts in 10 on a slider but the database entry
is 8 or 12 then 2 is added to the score of that item. The lower the
score the closer the match. If all the sliders are the same for an
item as those the user enters the score will be 0. Is there a quick
way for MYSQL to do this or is it a matter of doing the maths in the
application rather than via a simple query?

I hope this makes sense. I'm looking to find the quickest and simplest
way to do search like this through a database of about 5000 items.


So if I understood you correctly.. You would have table like this:

| item |
--------
|id
|slider1
|slider2
|slider3
|slider4
|slider5
|slider6

And you would have 5000 rows in that table. Then user gives you 6 values
that represent the values of slider1-6. Then you will calculate the
difference value for each item and return closest matches.

Difference value will be calculated like this:
If item in database has values:
1,10,5,10,10,10

And user enters values: 1,5,10,15,10,10

You would get difference:
abs(1-1) + abs(5-10) + abs(10-5) + abs(15-10) + abs(10-10) + abs(10-10)
= abs(0) + abs(-5) + abs(5) + abs(10) + abs(0) + abs(0)
= 0 + 5 + 5 + 10 + 0 + 0
= 20

If this is the case, then let's have a look at the example I created:
#################

create table item(id int, value1 int,value2 int,value3 int,value4
int,value5 int, value6 int);

insert into item values(1,1,1,1,1,1,1);
insert into item values(2,10,10,10,10,10,10);
insert into item values(3,10,10,10,10,10,10);
insert into item values(4,11,10,10,10,10,10);

select * from item;
+------+--------+--------+--------+--------+--------+--------+
| id | value1 | value2 | value3 | value4 | value5 | value6 |
+------+--------+--------+--------+--------+--------+--------+
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 10 | 10 | 10 | 10 | 10 | 10 |
| 3 | 10 | 10 | 10 | 10 | 10 | 10 |
| 4 | 11 | 10 | 10 | 10 | 10 | 10 |
+------+--------+--------+--------+--------+--------+--------+
4 rows in set (0.00 sec)

# So if you search match for these values( 1,1,3,1,1,1 ):

select id,
(abs(value1-1)+abs(value2-1)+abs(value3-3)+abs(value4-1)+abs(value5-1)+abs(value6-1))
as value from item order by value limit 1;

+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
+------+-------+

Please note, that it is possible that several items can have same value,
and this only returns first one. ( You asked only for one. )

Without "limit" the query would return them all, like this:
+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
| 2 | 52 |
| 3 | 52 |
| 4 | 53 |
+------+-------+

However, what comes to speed issues. The speed of this query will
increase as the amount of rows increase in the database, since query
will have to do calculations for each row every time it is executed.
5000 rows? I don't think you will get problems with speed, but better
try it out to be sure.

I am not an expert what comes to mathematics, so I'm not sure if there
is a better way to calculate the difference value. Searching for exact
match would be faster.

Jul 19 '05 #3
Aggro <sp**********@yahoo.com> wrote in message news:<Cg***************@read3.inet.fi>...
stew dean wrote:
I am very new to mysql so please forgive me if I may be asking
something obvious - I'm also not likely to fully understand the answer
but if I know it can be done and someone could point me in the right
direction I will spend the time to learn what I need to get things
going.

If I create a database can I do the following easily using MYSQL (with
PHP or similair)?

I want to set up a system where a user sets sliders to different
positions to select things. Each item will have a value for each
sliders stored with it and then as the user changes the sliders I want
to be able to check the database to find the closest match to the
users sliders.

In a bit more detail, I have six sliders, each with a value between
one and 20, I want my system to compare the users settings to each
entry in the database and find the least different. Each item would be
scored - so if the user puts in 10 on a slider but the database entry
is 8 or 12 then 2 is added to the score of that item. The lower the
score the closer the match. If all the sliders are the same for an
item as those the user enters the score will be 0. Is there a quick
way for MYSQL to do this or is it a matter of doing the maths in the
application rather than via a simple query?

I hope this makes sense. I'm looking to find the quickest and simplest
way to do search like this through a database of about 5000 items.


So if I understood you correctly.. You would have table like this:

| item |
--------
|id
|slider1
|slider2
|slider3
|slider4
|slider5
|slider6

And you would have 5000 rows in that table. Then user gives you 6 values
that represent the values of slider1-6. Then you will calculate the
difference value for each item and return closest matches.

Difference value will be calculated like this:
If item in database has values:
1,10,5,10,10,10

And user enters values: 1,5,10,15,10,10

You would get difference:
abs(1-1) + abs(5-10) + abs(10-5) + abs(15-10) + abs(10-10) + abs(10-10)
= abs(0) + abs(-5) + abs(5) + abs(10) + abs(0) + abs(0)
= 0 + 5 + 5 + 10 + 0 + 0
= 20


<trim rest of exellent example>

Thanks for the reply Aggro. You answer works in the way I was going to
do it - query all the items, do the maths as about then put the scores
in an table/array and then sort for the matches, although the example
using database specific terms was very helpful. I have yet to use SQL
to do this but my previous perl version got slower and slower the more
items I put in.

I guess I'm wondering if someone out there knows of a quicker way to
do it. I even contemplated prebuilding each combination of slider but
the idea is to eventualy have about eight sliders with a 256
resolution and have them optional.

Perhaps I should be asking this question in a maths group. Anyone have
any recommendations - I get this nagging feeling there must be a
faster way to do this.

Thanks again,

Stew Dean
Jul 19 '05 #4
Aggro <sp**********@yahoo.com> wrote in message news:<Cg***************@read3.inet.fi>...
stew dean wrote:
I am very new to mysql so please forgive me if I may be asking
something obvious - I'm also not likely to fully understand the answer
but if I know it can be done and someone could point me in the right
direction I will spend the time to learn what I need to get things
going.

If I create a database can I do the following easily using MYSQL (with
PHP or similair)?

I want to set up a system where a user sets sliders to different
positions to select things. Each item will have a value for each
sliders stored with it and then as the user changes the sliders I want
to be able to check the database to find the closest match to the
users sliders.

In a bit more detail, I have six sliders, each with a value between
one and 20, I want my system to compare the users settings to each
entry in the database and find the least different. Each item would be
scored - so if the user puts in 10 on a slider but the database entry
is 8 or 12 then 2 is added to the score of that item. The lower the
score the closer the match. If all the sliders are the same for an
item as those the user enters the score will be 0. Is there a quick
way for MYSQL to do this or is it a matter of doing the maths in the
application rather than via a simple query?

I hope this makes sense. I'm looking to find the quickest and simplest
way to do search like this through a database of about 5000 items.


So if I understood you correctly.. You would have table like this:

| item |
--------
|id
|slider1
|slider2
|slider3
|slider4
|slider5
|slider6

And you would have 5000 rows in that table. Then user gives you 6 values
that represent the values of slider1-6. Then you will calculate the
difference value for each item and return closest matches.

Difference value will be calculated like this:
If item in database has values:
1,10,5,10,10,10

And user enters values: 1,5,10,15,10,10

You would get difference:
abs(1-1) + abs(5-10) + abs(10-5) + abs(15-10) + abs(10-10) + abs(10-10)
= abs(0) + abs(-5) + abs(5) + abs(10) + abs(0) + abs(0)
= 0 + 5 + 5 + 10 + 0 + 0
= 20


<trim rest of exellent example>

Thanks for the reply Aggro. You answer works in the way I was going to
do it - query all the items, do the maths as about then put the scores
in an table/array and then sort for the matches, although the example
using database specific terms was very helpful. I have yet to use SQL
to do this but my previous perl version got slower and slower the more
items I put in.

I guess I'm wondering if someone out there knows of a quicker way to
do it. I even contemplated prebuilding each combination of slider but
the idea is to eventualy have about eight sliders with a 256
resolution and have them optional.

Perhaps I should be asking this question in a maths group. Anyone have
any recommendations - I get this nagging feeling there must be a
faster way to do this.

Thanks again,

Stew Dean
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: philippe Tressard | last post by:
Hi, I've 2 instances of apache 1.3.27 with php 4.2.3 and mysql 3.23.42 running on the same server. The 1st apache listens the port number 80 and communicates with the 1st mysql with the...
2
by: Cathy Hui | last post by:
Do u know why do i get the following message when trying to build the MySql-Python (1.2.0) on my Solaris 8 system? (with mysql 4.0.21 and python 2.4). thanks! error mesg: ld: fatal:...
0
by: Miguel Perez | last post by:
Hi Guys: Does anyone know how come I'm getting a lot of mysql processes when I start mysql server for the first time. When I type top it showed the following: 5:33pm up 15 min, 4 users, ...
0
by: Bill Hernandez | last post by:
Hi, I've been writing software on the mac since 1987, but am brand new at unix/php/mysql, and that's where I'm headed so I'm reading everything I can get my hands on, but like anything else...
0
by: sanjay gupta | last post by:
Hi All, I have recently installes mysql -4.0.0.1 on my redhat 7.1 machine . After installing when enter into mysql it gives me message " Access denied for user root@local host .......using...
0
by: Yun Guan | last post by:
Hello mysql gurus, I am trying to run perl on mysql database on Red Hat box. I want to install DBI and DBD:mysql using CPAN: perl -MCPAN -e shell cpan>install DBI The above succeeded, but...
3
by: Nick | last post by:
hi, all I have a red hat 9 and looks like it had mysql 3.2.54 installed before, but, actually not, i can not find the install folder and I can not uninstall it. So I am trying to install/upgrade...
74
by: John Wells | last post by:
Yes, I know you've seen the above subject before, so please be gentle with the flamethrowers. I'm preparing to enter a discussion with management at my company regarding going forward as either...
2
by: trihanhcie | last post by:
I m currently working on a Unix server with a fedora 3 as an os My current version of mysql is 3.23.58. I'd like to upgrade the version to 5.0.18. After downloading from MYSQL.COM the package on...
3
by: tulika dutta | last post by:
after 8 hrs my MySQL connection with JDBC gets time out. It gives the error java.sql.SQLException: No operations allowed after connection closed.Connection was implicitly closed due to underlying...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.