Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Write subquery in Joomla 3 using JDatabase method.

Here is an example of write subquery in Joomla! 3 using JDatabase method.

<?php
// Initialize variables.
$db       = JFactory::getDbo();
$subQuery = $db->getQuery(true);
$query    = $db->getQuery(true);

// Create the base subQuery select statement.
$subQuery->select('*')
	->from($db->quoteName('#__sub_table'))
	->where($db->quoteName('subTest') . ' = ' . $db->quote('1'));

// Create the base select statement.
$query->select('*')
	->from($db->quoteName('#__table'))
	->where($db->quoteName('state') . ' = ' . $db->quote('1'))
	->where($db->quoteName('subCheckIn') . ' IN (' . $subQuery . ')')
	->order($db->quoteName('ordering') . ' ASC');

// Set the query and load the result.
$db->setQuery($query);

try
{
	$result = $db->loadObjectList();
}
catch (RuntimeException $e)
{
	throw new RuntimeException($e->getMessage(), $e->getCode());
}

By 6|_||\|74|\| |>4731

@AdamMadrzejewski

This comment has been minimized.

Copy link

commented May 12, 2015

Great! Thanks for this gist.

@AdamMadrzejewski

This comment has been minimized.

Copy link

commented Oct 20, 2015

Wow, somehow I returned here again and saw my comment! Great gist man ;)

@gunjanpatel

This comment has been minimized.

Copy link
Owner Author

commented Jul 6, 2016

Welcome @AdamMadrzejewski Glade that it is helpful to you.

@NhatNam98

This comment has been minimized.

Copy link

commented Nov 26, 2018

Thanks Man , have a good day

@application2000

This comment has been minimized.

Copy link

commented May 9, 2019

Cool! Thanks! Follows K.I.S.S principle. Simple is beautiful. Nice one! Have a nice day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.