954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Custom fields

What I'm trying to do is produce a query that will use custom fields. I have the following tables:
entries

id
title
slug
user_id
section_id


entries_values

id
entry_id
sections_fields_id
value


sections_fields

id
name
value
section_id


So what I want to do is run a select beginning with entries and from there I want to be able to output the custom fields using something similar toentry.body where entry.body will be a custom field. I'm not sure if I'm being clear or not, but I'm not sure how to explain it so here's an example:
entries

id = 1
title = Test
slug = test


sections_fields

id = 1
name = body


entries_values

entry_id = 1
sections_fields_id = 1
value = This should be outputted by using entry.body


I'd like to perform this using one query instead of running multiple queries. Anyone have any guidance?

drewtemp
Newbie Poster
2 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Using your example, the following query should work:

-- get the required columns using aliases where necessary
SELECT e.id AS entry_id, sf.id AS sections_fields_id, sf.name AS value
-- from the entries table
FROM entries e
-- join to the section_fields table
JOIN sections_fields sf
-- describe how to join tables
ON e.section_id = sf.section_id
-- limit the returned results according to criteria?
WHERE e.id = 1 AND sf.id = 1


HTH :)

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You