问题
In my Joomla component I have two table:
Table1: #__com_units
Id | Building | Floor |unit_number | posx | posy | mood
-------------------------------------------------------
1 | 01 | 01 | 001A | 100 | 200 |Rented
2 | 01 | 01 | 002A | 101 | 202 |Available
3 | 01 | 01 | 003A | 102 | 204 |Available
4 | 01 | 01 | 004A | 103 | 206 |Available
5 | 01 | 01 | 005A | 104 | 208 |Rented
6 | 01 | 01 | 006A | 103 | 206 |Available
7 | 01 | 01 | 007A | 104 | 208 |Rented
8 | 01 | 01 | 008A | 103 | 206 |Rented
9 | 01 | 01 | 009A | 104 | 208 |Rented
10 | 01 | 01 | 010A | 103 | 206 |Available
Table1: #__com_reservations
Id | Building | Floor |unit | confirmation
--------------------------------------------------
1 | 01 | 01 | 002A | NO
2 | 01 | 01 | 003A | YES
3 | 01 | 01 | 004A | NO
4 | 01 | 01 | 006A | NO
5 | 01 | 01 | 010A | YES
i want to get query to show units as buttons which "mood=Rented" and "confirmation=YES" but its not working:
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$jinput = JFactory::getApplication()->input;
$query->select($db->quoteName(array(
'b.unit_number',
'a.confirmation',
'b.posx',
'b.id',
'a.unit',
'b.posy',
'b.mood'
)));
$query->from($db->quoteName('#__com_reservations','a' ))
->join('INNER', $db->quoteName('#__com_units', 'b') . ' ON (' . $db->quoteName('b.unit_number') . ' = ' . $db->quoteName('a.unit') . ')');
$query->where($db->quoteName('b.floor')." = ".$db->quoteName('a.floor'),'AND')
->where($db->quoteName('b.building')." = ".$db->quoteName('a.building'),'AND')
->where($db->quoteName('a.confirmation')." = ".$db->quote('YES'));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result) {
echo '<button class=" ' . $result->mood . ' ' . $result->posx . ' ' . $result->posy . '" value=' . $result->id . ' disabled> ' . $result->Unit_number . '</button>';
}
?>
回答1:
I don't know the Joomla but from your code it looks like you are trying to join #__com_units table with #__com_reservations using #__com_units.unit_number and #__com_reservations.id. Both fields have different data so you are not joining the tables properly. Tables should be joins using units field as #__com_units.unit_number = #__com_reservations.unit_number
回答2:
SELECT * FROM `#__com_reservations` A
JOIN `#__com_units` B
ON A.unit = B.unit_number
AND A.building = B.building
AND A.floor = B.floor
WHERE A.confirmation = 'Yes'
AND B.mood = 'Available'
Even though i'm not familiar with joomla hope this mysql query will be helpful.
回答3:
Here is solution:
->join('INNER', $db->quoteName('#__com_units', 'b') . ' ON (' . $db->quoteName('b.id') . ' = ' . $db->quoteName('a.unit') . ')');
来源:https://stackoverflow.com/questions/37589564/php-query-inner-join-tables