personal blog related to technology, coding and publishing.
Archive for February, 2007
Even more SQL madness! (It never ends!)
Feb 23rd
So I’m still working on all that SQL stuff – now it’s up to figuring out how to get the multiple keyword search going again.
I have a method, not sure how efficient it is (I’m going to run some tests) but so far it’s working.
-
/****** GETTING A LIST OF IMAGES BACK *****/
-
SELECT
-
it.Filename,
-
it.Record_ID AS Record_ID,
-
it.Path,
-
it.File_Description,
-
"image_server" AS database_name,
-
"1" AS using_databases,
-
"1" AS databaseID
-
FROM
-
image_server.Item_Table it
-
JOIN
-
image_server.Item_Keyword ik
-
ON
-
ik.Item_ID = it.Record_ID
-
JOIN
-
image_server.Keyword k
-
ON
-
k.Record_ID = ik.Keyword_ID
-
WHERE
-
k.Keyword = ‘dog’
-
AND
-
ik.Item_ID IN (
-
SELECT
-
ik.Item_ID
-
FROM
-
image_server.Item_Keyword ik
-
WHERE
-
ik.Keyword_ID = (SELECT
-
k.Record_ID
-
FROM
-
image_server.Keyword k
-
WHERE
-
k.Keyword = ‘cat’) )
Update: I figured I’d use that search method above for now until I find a better method.
That’s not the end of my troubles, however. I’ve got some issue with a JOIN statement that’s not returning the right result set (the right row):
-
SELECT
-
sl.id,
-
sl.userID,
-
sl.insertdate,
-
sl.searchString,
-
sl.galleryID,
-
sl.imageType,
-
sl.imageColor,
-
sl.databaseID,
-
sl.using_database,
-
sg.Name,
-
db.dbcommonname
-
FROM
-
CE_search_log2 sl
-
JOIN
-
Saved_Galleries sg
-
ON
-
sl.galleryID = sg.Record_ID
-
JOIN
-
CE_dblist db
-
ON
-
db.id = sl.databaseID
-
WHERE
-
sl.userID = ‘cedge’
-
ORDER BY
-
sl.insertdate DESC
-
LIMIT 0,1
-
– returns search ID # 186
-
-
SELECT * FROM CE_search_log2 ORDER BY insertdate DESC LIMIT 0,1
-
– returns search ID # 194
If you notice, the first query returns the CE_search_log.id row of 186. The second query returns the correct row of 194. I think it might be because of my join statement, but I’m not sure. Argh, frustrating!
Update (again): Looks like it was a JOIN problem. I needed a LEFT JOIN for the CE_dblist line. D’oh!
More SQL Madness!
Feb 13th
OK, so here’s the new issue:
I’ve got a resultset with a screwy FilePath column (normally, the FilePath column has a full system-path (mac-style) stored in it). The screwy results need to have their path replaced with the correct data, without losing the actual filename that’s stored in the FilePath column.
Here’s what I’m looking at:
PMS_1106:Photos:001P1106PM.JPG
What I need to do is strip out everything but the filename (001P1106PM.JPG) and replace it with the new full system-path data.The filenames are unique, but not consistent in naming (length, letters, underscores, etc). I suppose I could work some regex magic (any a-z or A-Z 0-9 and _, – character(s), followed by a ‘.’ and [JPG|EPS|ext?]).
Hrm. might just have to experiment with this one.
Mathematics for “Beer Goggles”
Feb 9th
From How Stuff Works:
Many of us have heard of the so-called “beer goggle” effect. It’s the phenomenon that occurs when someone’s had a few alcoholic drinks and suddenly, all of those people who looked semi-attractive on entering the bar look really, really appealing. Scientists have shown that it’s not just a lowering of standards — alcohol actually stimulates the part of the brain that judges facial attractiveness.
The formula:
| β = | √L x (Vo)2 |
- where:
- An is the number of servings of alcohol
- S is the smokiness of the area on a scale of 0 – 10
- L is the lighting level of the area, measured in candelas per square meter, in which 150 is normal room lightning
- Vo is Snellen visual acuity, in which 6/6 is normal and 6/12 is the lower limit at which someone is able to drive
- d is the distance between the observer and the observed, measured in meters
Source: BBC News
The formula works out a “beer goggle” score ranging from 1 to 100+. When β = 1, the observer is perceiving the same degree of beauty he or she would perceive in a sober state. At 100+, everybody in the room is a perfect 10.
Searching multiple databases
Feb 9th
So I’m working on some code that will allow me to search multiple databases in a single query, returning a resultset.
Note: this is MySQL
-
– Searching the "clipper" database
-
– for any image that has ‘dog%’ as a keyword
-
SELECT
-
S.Filename,
-
S.Record_ID,
-
S.Path,
-
S.File_Description
-
FROM
-
clipper.Item_Table S,
-
clipper.Keyword C1,
-
clipper.Item_Keyword O1
-
WHERE
-
S.Record_ID = O1.Item_ID
-
AND
-
O1.Keyword_ID = C1.Record_ID
-
AND
-
C1.Keyword LIKE ‘dog%’
-
GROUP BY
-
S.Record_ID
-
LIMIT 0,30
What I’m having a tough time with is the clipper.columnName part. Each search may have many, many C[n] and O[n] values (these are the keyword items as they relate to the joins). If I have clipper.THIS and clipper.THAT, then how do I add image_server.THIS and image_server.THAT*?
I’m thinkink I might just loop the queries and pool all the results together into one XML resultset.
There should be a more efficient way, however, right?
* “clipper” and “image_server” are two databases where images are stored.
Could this be the answer?
-
– Searching the "clipper" AND "image_server"
-
– databases for any image that has ‘dog%’ as a keyword
-
SELECT
-
S1.Filename,
-
S1.Record_ID,
-
S1.Path,
-
S1.File_Description,
-
S2.Filename,
-
S2.Record_ID,
-
S2.Path,
-
S2.File_Description
-
FROM
-
clipper.Item_Table S1,
-
clipper.Keyword C1,
-
clipper.Item_Keyword O1,
-
image_server.Item_Table S2,
-
image_server.Keyword C2,
-
image_server.Item_Keyword O2
-
WHERE
-
S1.Record_ID = O1.Item_ID
-
AND
-
S2.Record_ID = O2.Item_ID
-
AND
-
O1.Keyword_ID = C1.Record_ID
-
AND
-
O2.Keyword_ID = C2.Record_ID
-
AND
-
C1.Keyword LIKE ‘dog%’
-
AND
-
C2.Keyword LIKE ‘dog%’
-
GROUP BY
-
S1.Record_ID,
-
S2.Record_ID
Solved:
Thanks to Greg Miley, the problem has been solved:
-
SELECT
-
cit.Filename,
-
cit.Record_ID AS Record_ID,
-
cit.Path,
-
cit.File_Description,
-
"clipper" AS database_name
-
FROM
-
clipper.Item_Table cit
-
JOIN
-
clipper.Item_Keyword cik
-
ON cik.Item_ID = cit.Record_ID
-
JOIN
-
clipper.Keyword ck
-
ON ck.Record_ID = cik.Keyword_ID
-
WHERE
-
ck.Keyword LIKE ‘dog%’
-
OR
-
ck.Keyword LIKE ‘cat%’
-
-
UNION
-
SELECT
-
isit.Filename,
-
isit.Record_ID AS Record_ID,
-
isit.Path,
-
isit.File_Description,
-
"image_server" AS database_name
-
FROM
-
image_server.Item_Table isit
-
JOIN
-
image_server.Item_Keyword isik
-
ON isik.Item_ID = isit.Record_ID
-
JOIN
-
image_server.Keyword isk
-
ON isk.Record_ID = isik.Keyword_ID
-
WHERE
-
isk.Keyword LIKE ‘dog%’
-
OR
-
isk.Keyword LIKE ‘cat%’