简体   繁体   中英

php Query join tables

In my Joomla component I have two table:

Table1: #__com_units

 Id | unit | posx | posy 
 ------------------------
  1 | 001A | 100 | 200
  2 | 002A | 101 | 202
  3 | 003A | 102 | 204
  4 | 004A | 103 | 206
  5 | 005A | 104 | 208

Table1: #__com_reservations

 Id | unit |     From    |     To    | Mood (dropdown with value of Avaliable and Rented)
 --------------------------------------------
  1 | 001A | YYYY-MM-DD | YYYY-MM-DD |Available
  2 | 002A | YYYY-MM-DD | YYYY-MM-DD |Rented
  3 | 003A | YYYY-MM-DD | YYYY-MM-DD |Available
  3 | 004A | YYYY-MM-DD | YYYY-MM-DD |Available
  3 | 005A | YYYY-MM-DD | YYYY-MM-DD |Rented

i want two get two query to show the result as buttons, but its not working:

1) One for Rented units

  $db = JFactory::getDbo();
  $query = $db->getQuery(true);
  $jinput = JFactory::getApplication()->input;
  $query->select($db->quoteName(array('id', 'unit', 'mood', 'posx', 'posy')));
  $query->from($db->quoteName('#__com_units', '#__com_reservations'));
  $query->where($db->quoteName('unit')." = ".$db->quote('1'),'AND')     
  ->where($db->quoteName('mood')." = ".$db->quote('rented'));
  $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  . '</button>';
 }

2)One for Avaliable units

  $db = JFactory::getDbo();
  $query = $db->getQuery(true);
  $jinput = JFactory::getApplication()->input;
  $query->select($db->quoteName(array('id', 'unit', 'mood', 'posx', 'posy')));
  $query->from($db->quoteName('#__com_units', '#__com_reservations'));
  $query->where($db->quoteName('unit')." = ".$db->quote('1'),'AND')     
  ->where($db->quoteName('mood')." = ".$db->quote('available'));
  $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  . '</button>';
 }

3)And what if i want to have one query for rented units when they have valid date range?

Your query should be like this if you want to get result from two tables combined

$db = JFactory::getDbo();
  $query = $db->getQuery(true);
  $jinput = JFactory::getApplication()->input;
  $query->select($db->quoteName(array('a.unit', 'b.mood', 'a.posx', 'a.posy')));

  $query->from($db->quoteName('#__com_units', 'a'));
  $query->join('INNER', $db->quoteName('#__com_reservations', 'b') . ' ON (' . $db->quoteName('a.unit') . ' = ' . $db->quoteName('b.unit') . ')');

REST OF YOUR QUERY You are doing unit = 1, I dont know how as unit is given as 001A, 002A etc. If you follow the above syntax there wont be any error.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM