Ask a Question related to MySQL, Design and Development.
-
Justin Koivisto #1
Select, Join & field values - 2 tables
I had originally posted this in alt.comp.databases.mysql with no replies
(I thought I had posted it here, my bad). Hopefully one of you bright
individuals could shed some light on this for me:
Firstly, here's the example structure and data:
+---------------+
| log |
+---------------+
| id |
| approval_date |
| approved_by |
| page_title |
+---------------+
Data:
4, '2006-01-01 08:32:17', 'admin3', 'index'
....
+---------------+
| content |
+---------------+
| page_title |
| page_uri |
+---------------+
Data:
'PREFIX_index', 'index.html'
'index', 'index.html'
'ARCH_index', 'index.html'
What I want to do is select log.approval_date, log.approved_by and
content.page_uri for the records... The twist is that I want to join the
tables based on the page_title where they will be *different* values in
each table.
For instance, I want to get the following:
'2006-01-01 08:32:17', 'admin3', 'index.html'
This would require that the following would be the constraint for the JOIN:
CONCAT('PREFIX_', log.page_title) = content.page_title
This is the first thing I tried:
SELECT
content.page_uri
, log.approval_date
, log.approved_by
, REPLACE(content.page_title,'PREFIX_','') as xx
FROM content
INNER JOIN log
ON log.page_title = xx
WHERE
content.page_title LIKE 'PREFIX_%'
*** MySQL said: #1054 - Unknown column 'xx' in 'on clause'
Then I tried this:
SELECT
content.page_uri
, log.approval_date
, log.approved_by
FROM content
INNER JOIN log
ON CONCAT('PREFIX_', log.page_title ) = content.page_title
WHERE
content.page_title LIKE 'PREFIX_%'
.... this returned data, but looks like it's pretty much everything in
the log table coupled with the matching entries from the content table
(from the WHERE clause).
Can anyone get me in the right direction for this?
BTW - mysql 3.23.58 is one of the targeted systems, but I am also
interested in answers for newer versions as well.
Thanks
--
Justin Koivisto, ZCE - [email]justin@koivi.com[/email]
[url]http://koivi.com[/url]
Justin Koivisto Guest
-
Listing join from tables...
Hello all, I have a page with query from two tables... And am having problems listing values out of both tables... The query is : <cfquery... -
New to Joines - Inner Join on 4 Tables
I have four tables that I'm querying, I'm selecting all fields with the exception of the key_ID which is the field I'm joining on. Because I have... -
HELP.. Recordset JOIN tables
okay.. im new to this, and need some help, as i have spent all bank holiday trying to sort this. I have 1 database, with 2 tables. table 1 =... -
trying to update a table after making a join select query on two tables
here is my problem distilled down I need to be able to change the title of a book associated with an author there are 2 tables "author" and "books"... -
SELECT from multiple tables (not join though)
Hi all, I have another question, I hope it isn't too basic. ^.^ I want to do a select from multiple tables but not join them. What I am trying... -
Jerry Stuckle #2
Re: Select, Join & field values - 2 tables
Justin Koivisto wrote:
Justin,> I had originally posted this in alt.comp.databases.mysql with no replies
> (I thought I had posted it here, my bad). Hopefully one of you bright
> individuals could shed some light on this for me:
>
> Firstly, here's the example structure and data:
>
> +---------------+
> | log |
> +---------------+
> | id |
> | approval_date |
> | approved_by |
> | page_title |
> +---------------+
>
> Data:
> 4, '2006-01-01 08:32:17', 'admin3', 'index'
> ...
>
> +---------------+
> | content |
> +---------------+
> | page_title |
> | page_uri |
> +---------------+
>
> Data:
> 'PREFIX_index', 'index.html'
> 'index', 'index.html'
> 'ARCH_index', 'index.html'
>
>
> What I want to do is select log.approval_date, log.approved_by and
> content.page_uri for the records... The twist is that I want to join the
> tables based on the page_title where they will be *different* values in
> each table.
>
> For instance, I want to get the following:
>
> '2006-01-01 08:32:17', 'admin3', 'index.html'
>
> This would require that the following would be the constraint for the JOIN:
> CONCAT('PREFIX_', log.page_title) = content.page_title
>
> This is the first thing I tried:
> SELECT
> content.page_uri
> , log.approval_date
> , log.approved_by
> , REPLACE(content.page_title,'PREFIX_','') as xx
> FROM content
> INNER JOIN log
> ON log.page_title = xx
> WHERE
> content.page_title LIKE 'PREFIX_%'
>
> *** MySQL said: #1054 - Unknown column 'xx' in 'on clause'
>
> Then I tried this:
> SELECT
> content.page_uri
> , log.approval_date
> , log.approved_by
> FROM content
> INNER JOIN log
> ON CONCAT('PREFIX_', log.page_title ) = content.page_title
> WHERE
> content.page_title LIKE 'PREFIX_%'
>
> ... this returned data, but looks like it's pretty much everything in
> the log table coupled with the matching entries from the content table
> (from the WHERE clause).
>
> Can anyone get me in the right direction for this?
>
> BTW - mysql 3.23.58 is one of the targeted systems, but I am also
> interested in answers for newer versions as well.
>
> Thanks
>
Well, it's doing exactly like you said.
You specify in your inner join that content.page_title must start with
'PREFIX_'. Your where clause then specifies you display those rows
which have content.page_title starting with 'PREFIX_'.
IOW, your INNER JOIN and your WHERE clause both specify the same rows.
But I do have a problem with this and performance. You're asking MySQL
to contactenate 'PREFIX_' to EVERY ROW in content.page title before
making a match. This will be a huge amount of overhead if you have a
very large table. And MySQL probably won't be able to use indexes on
it, so you'll do a full table scan.
I think you'll be better separating the PREFIX_ and the rest of the
content.page_title column into two different columns when you store the
data in the table. That way MySQL will be able to use indexes if
appropriate and not have to concatenate every row.
If this doesn't answer your question, can you give us a little more
information, like what is in those columns and just what you're expecting?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
[email]jstucklex@attglobal.net[/email]
==================
Jerry Stuckle Guest
-
Bill Karwin #3
Re: Select, Join & field values - 2 tables
"Justin Koivisto" <justin@koivi.com> wrote in message
news:ebKdnaneZtx1BG3enZ2dnUVZ_sGdnZ2d@onvoy.com...The WHERE clause in this query is redundant. The value in> For instance, I want to get the following:
>
> '2006-01-01 08:32:17', 'admin3', 'index.html'
>
> SELECT
> content.page_uri
> , log.approval_date
> , log.approved_by
> FROM content
> INNER JOIN log
> ON CONCAT('PREFIX_', log.page_title ) = content.page_title
> WHERE
> content.page_title LIKE 'PREFIX_%'
content.page_title must begin with 'PREFIX_' if it matches another string
which has 'PREFIX_' prepended.
Other than that, that query should return what you described. Am I missing
something?
Regards,
Bill K.
Bill Karwin Guest
-
Justin Koivisto #4
Re: Select, Join & field values - 2 tables
Jerry Stuckle wrote:
some days (or months) I just can't function... ;)> Justin Koivisto wrote:>>> I had originally posted this in alt.comp.databases.mysql with no replies
>> (I thought I had posted it here, my bad). Hopefully one of you bright
>> individuals could shed some light on this for me:
>>
>> Firstly, here's the example structure and data:
>>
>> +---------------+
>> | log |
>> +---------------+
>> | id |
>> | approval_date |
>> | approved_by |
>> | page_title |
>> +---------------+
>>
>> Data:
>> 4, '2006-01-01 08:32:17', 'admin3', 'index'
>> ...
>>
>> +---------------+
>> | content |
>> +---------------+
>> | page_title |
>> | page_uri |
>> +---------------+
>>
>> Data:
>> 'PREFIX_index', 'index.html'
>> 'index', 'index.html'
>> 'ARCH_index', 'index.html'
>>
>>
>> What I want to do is select log.approval_date, log.approved_by and
>> content.page_uri for the records... The twist is that I want to join the
>> tables based on the page_title where they will be *different* values in
>> each table.
>>
>> For instance, I want to get the following:
>>
>> '2006-01-01 08:32:17', 'admin3', 'index.html'
>>
>> This would require that the following would be the constraint for the
>> JOIN:
>> CONCAT('PREFIX_', log.page_title) = content.page_title
>>
>> This is the first thing I tried:
>> SELECT
>> content.page_uri
>> , log.approval_date
>> , log.approved_by
>> , REPLACE(content.page_title,'PREFIX_','') as xx
>> FROM content
>> INNER JOIN log
>> ON log.page_title = xx
>> WHERE
>> content.page_title LIKE 'PREFIX_%'
>>
>> *** MySQL said: #1054 - Unknown column 'xx' in 'on clause'
>>
>> Then I tried this:
>> SELECT
>> content.page_uri
>> , log.approval_date
>> , log.approved_by
>> FROM content
>> INNER JOIN log
>> ON CONCAT('PREFIX_', log.page_title ) = content.page_title
>> WHERE
>> content.page_title LIKE 'PREFIX_%'
>>
>> ... this returned data, but looks like it's pretty much everything in
>> the log table coupled with the matching entries from the content table
>> (from the WHERE clause).
>>
>> Can anyone get me in the right direction for this?
>>
>> BTW - mysql 3.23.58 is one of the targeted systems, but I am also
>> interested in answers for newer versions as well.
> Well, it's doing exactly like you said.
>
> You specify in your inner join that content.page_title must start with
> 'PREFIX_'. Your where clause then specifies you display those rows
> which have content.page_title starting with 'PREFIX_'.
>
> IOW, your INNER JOIN and your WHERE clause both specify the same rows.
gee - without using the indexes, it's totally useless then.> But I do have a problem with this and performance. You're asking MySQL
> to contactenate 'PREFIX_' to EVERY ROW in content.page title before
> making a match. This will be a huge amount of overhead if you have a
> very large table. And MySQL probably won't be able to use indexes on
> it, so you'll do a full table scan.
I think I will just leave it the way I have it now using loops and> I think you'll be better separating the PREFIX_ and the rest of the
> content.page_title column into two different columns when you store the
> data in the table. That way MySQL will be able to use indexes if
> appropriate and not have to concatenate every row.
additional queries. That way I am not running through the entire table
for the searches.
In this instance, I can't change the data structure, but on the next> If this doesn't answer your question, can you give us a little more
> information, like what is in those columns and just what you're expecting?
version of the system I have free reign, and will have *everything*
quite a bit different.
Thanks for the answer!
--
Justin Koivisto, ZCE - [email]justin@koivi.com[/email]
[url]http://koivi.com[/url]
Justin Koivisto Guest



Reply With Quote

