dptdb84606 2016-06-01 16:09 采纳率: 0%
浏览 46

php查询连接表

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?

  • 写回答

1条回答 默认 最新

  • dongpi3237 2016-06-02 12:11
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器