I'm building a Joomla template. For this I need to test/query two fields in the DB. I'm trying to get familiar with getDBO
class but stuck here.
These two queries do nearly the same. I need both variables $category
and $hasField
. How can merge these two queries into one? This is a bit redundant.
$db = JFactory::getDBO();
$id = JRequest::getInt('id');
$db->setQuery('
SELECT
#__categories.title
FROM
#__content,
#__categories
WHERE
#__content.catid = #__categories.id
AND
#__content.id = '.$id
);
$category = $db->loadResult();
$db->setQuery('
SELECT
#__attachments.filename,
#__attachments.parent_id
FROM
#__attachments
WHERE
#__attachments.parent_id =' . $id
);
$hasField = $db->loadResult();
You can try joining #__attachments
to the 1st query.
SELECT
#__categories.title,
#__attachments.filename,
#__attachments.parent_id
FROM
#__content,
#__categories,
#__attachments
WHERE
#__content.catid = #__categories.id
AND
#__attachments.parent_id = #__content.id
AND
#__content.id = $id
I might be wrong there as no place to test but it suppose to be like left joined:
SELECT
#__categories.title, atch.filename
FROM
#__content,
#__categories
LEFT JOIN
#__attachments AS atch ON atch.parent_id = #__content.id
WHERE
#__content.catid = #__categories.id
AND
#__content.id = '.$id
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.