简体   繁体   English

php查询内部联接表

[英]php Query INNER join tables

In my Joomla component I have two table: 在我的Joomla组件中,我有两个表:

Table1: #__com_units 表1:#__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 表1:#__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>';
    }
?>

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 . 我不知道Joomla,但是从您的代码看来,您似乎正在尝试使用#__com_units.unit_number#__com_reservations.id#__com_units表与#__com_reservations #__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 表格应使用单位字段联接为#__com_units.unit_number = #__com_reservations.unit_number

 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. 即使我不熟悉joomla,也希望此mysql查询对您有所帮助。

这是解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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