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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题