Jump to content

[SOLVED] Need help with query (3 different tables).


play_

Recommended Posts

blog

+-------------+-------------+------+-----+---------------------+----------------+
| Field       | Type        | Null | Key | Default             | Extra          |
+-------------+-------------+------+-----+---------------------+----------------+
| blogID      | int(11)     | NO   | PRI | NULL                | auto_increment | 
| title       | varchar(30) | NO   |     | NULL                |                | 
| content     | text        | NO   |     | NULL                |                | 
+-------------+-------------+------+-----+---------------------+----------------+

 

tags

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| tagID | int(11)     | NO   | PRI | NULL    | auto_increment | 
| tag   | varchar(20) | NO   |     | NULL    |                | 
+-------+-------------+------+-----+---------+----------------+

 

ref_blog_tag

+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| refID  | int(11) | NO   | PRI | NULL    | auto_increment | 
| blogID | int(11) | NO   |     | NULL    |                | 
| tagID  | int(11) | NO   |     | NULL    |                | 
+--------+---------+------+-----+---------+----------------+

 

Those are the 3 tables i'm having trouble with.

 

What I am trying to do is output the blog entry, and its tag(s).

 

I've tried join queries but can't get it to work.

 

Can it all be done in one query? (i think this is unlikely because if so, all the tags to that blog entry would have to be returned as an array)

 

And if not, how could i make this work?

 

If it helps, here's my PHP code:

<?php

$statement = $sql->prepare("select * from blog order by blog.blogID desc");
$statement->execute();
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ) {
	// get tags for this entry
	$query = "
	select
		tags.tagID,
		tags.tag
	from
		tags			
	join
		ref_blog_tag
	on
		 ref_blog_tag.blogID = ?";

	$statement2 = $sql->prepare($query);
	$statement2->bindParam(1, $row['blogID']);
	$statement2->execute();
	echo '
	<div class="blog-entry">
		<div class="blog-date"><span class="blog-month">Oct</span>17</div>
		<div></div>
		<div style="text-align: justify; overflow: auto;">' . nl2br(stripslashes($row['content'])) . '</div>
	</div>';
}

 

Thanks and secret be told, i'm horrible with mysql.

Link to comment
Share on other sites

Alright i figured it out.

 

The second query from the post above should be

 

$query = "
	select
		tags.tagID,
		tags.tag
	from
		tags
	inner join ref_blog_tag ON ref_blog_tag.tagID = tags.tagID
	where
		ref_blog_tag.blogID = ?";

 

However, i am leaving this open for a bit just in case there is a way to do this all in just 1 query, and someone happens to show me how, or just tell me it can't be done.

Link to comment
Share on other sites

  • 3 weeks later...

Thanks.

 

I'm still trying to get this in one query. this time, not with blogs, but a site im creating for storing regular expressions.

 

The concept is just like the blog scheme above. I have a table that stores regular expressions, one that stores tags/categories it may be in (ie, 'phone', 'numbers'), and a reference table.

 

mysql> describe expressions;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| regexpID       | int(11)      | NO   | PRI | NULL    | auto_increment | 
| reg_exp        | varchar(255) | NO   |     | NULL    |                | 
| description    | varchar(255) | NO   |     | NULL    |                | 
| author         | varchar(20)  | NO   |     | NULL    |                | 
| date_submitted | datetime     | NO   |     | NULL    |                | 
+----------------+--------------+------+-----+---------+----------------+


 

 

mysql> describe tags;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| tagID        | tinyint(2)   | NO   | PRI | NULL    | auto_increment | 
| name         | varchar(50)  | NO   |     | NULL    |                | 
| display_name | varchar(100) | NO   |     | NULL    |                | 
+--------------+--------------+------+-----+---------+----------------+

 

 

mysql> describe ref_regexp_tag;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| refID    | int(11) | NO   | PRI | NULL    | auto_increment | 
| regexpID | int(11) | NO   |     | NULL    |                | 
| tagID    | int(11) | NO   |     | NULL    |                | 
+----------+---------+------+-----+---------+----------------+

 

 

This would be easy if a regular expression could have only 1 category/tag. But since it can have more than one, i had to create this 3 table schema.

 

What i am trying to select is, the regular expression (reg_exp) and the tags associated with it.

 

 

 

ps: im not demanding a a solution, just throwing it out here in case someone's easily got this down. don't want anyone spending too much time and energy on it :) i can always use the same solution i used in the first post.

Link to comment
Share on other sites

I don't think it's possible.

 

given the following query

select distinct
	expressions.regexpID,
	expressions.reg_exp
from
	expressions
inner join ref_regexp_tag on ref_regexp_tag.regexpID = expressions.regexpID
[/code[

when i print_r the results, i get

[code]
Array
(
    [0] => Array
        (
            [regexpID] => 1
            [reg_exp] => [:digit:]
        )

)

 

If i were to select all the tags associated with that reg_exp, i don't even see how it would show up in this array.

Link to comment
Share on other sites

Hi

 

This would give you one row per regular expression / name:-

 

SELECT a.regexpID, a.reg_exp, c.tagID, c.name, c.display_name
FROM expressions a
LEFT OUTER JOIN ref_regexp_tag b ON a.regexpID = b.regexpID
LEFT OUTER JOIN tags c ON b.tagID = c.tagID

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.