MySQL - Select 列 'Name' 根据用户的选择 其中列 'id' 和 'parent' 中的行彼此相等

MySQL - Select Column 'Name' according to user's choice Where rows in Column 'id' and 'parent' equal each other

当parent = 0表示类别

当 parent = 1 表示子类别 1 连接到类别 1 (id=1)

当 parent = 2 表示子类别 2 连接到类别 2 (id=2)

当父项...等直到 19 个类别(id=19 且父项=0)

我需要的是根据用户在类别字段中的选择,在子类别表单域中带上子类别的名称。 类别字段工作正常。

id      parent      name                            active
1       0           Arts & Entertainment            0
2       0           Automotive                      0
3       0           Business & Professional Serv.   1
4       0           Clothing & Accessories          0
5       0           Community & Government          0
6       0           Computers & Electronics         1
7       0           Construction & Contractors      0
8       0           Education                       0
9       0           Food & Dining                   0
10      0           Health & Medicine               0
11      0           Home & Garden                   0
12      0           Industry & Agriculture          0
13      0           Legal & Financial               1
14      0           Media & Communications          0
15      0           Personal Care & Services        0
16      0           Real Estate                     0
17      0           Shopping                        0
18      0           Sports & Recreation             0
19      0           Travel & Transportation         0
34      1           Acting Schools                  1
35      1           Aerial Photographers            1
36      1           Arcades & Amusements            1
37      1           Art Classes                     1
38      1           Art Galleries & Dealers         1
39      1           Art Schools                     1

1.这是类别字段的查询,它工作正常并为我们提供了用户的选择($judgePick)

$db->setQuery('SELECT name FROM #__professional_categ WHERE parent=0 AND active=1 ORDER BY name ASC');

2.This is the Query for the subcategory field trying to solve

$judgePick = JRequest::getVar('category');
$db = JFactory::getDBO();

$db->setQuery('SELECT `name` FROM `#__professional_categ` WHERE active = 1 AND (something here...) ORDER BY parent ASC, name ASC);

$result = $db->loadColumn();
 if(!$result){
echo "error";
} else {
    echo json_encode($result);
}

假设 1 - 要包含的 id ='.$db->quote($judgePick)

假设 2 - 对于 parent > 0 必须等于用户在假设 1 中选择的 id

预期结果

子类别字段仅根据用户在类别字段($judgePick)中的选择来命名,其中用户的选择 ID 等于父级。换句话说,例如艺术与娱乐是类别 (parent=0) 并且具有 (id =1),当用户在类别表单字段中选择它时,子类别表单字段应显示所有名称 (parent=1)

这个呢

$db->setQuery("SELECT name FROM #__professional_categ WHERE parent=$judgePick AND active=1 ORDER BY name ASC");

您要查找的可能是自连接:

SELECT x.name 
FROM #__professional_categ x
JOIN #__professional_categ y
  ON x.parent = y.id
WHERE y.name = ‘. $judgePick .‘
  AND x.parent = y.id
  AND x.active = 1

您可以在这里查看抽象样本的查询: http://www.sqlfiddle.com/#!9/ecc4bb/1/0

由于在您的代码中输入,您只能得到所选类别的名称,因此我们必须select 它的id 也在 table 中,然后我们可以找到 select 子类别的父 ID,并基于此 return 子类别的名称。

在 Joomla 语法中,您的代码和查询应如下所示:

$jinput = JFactory::getApplication()->input;
$judgePick = $jinput->get(‘category’);

$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

$query
  ->select('x.name')
  ->from($db->quoteName('#__professional_categ', 'x'))
  ->join('LEFT', $db->quoteName('#__professional_categ', 'y') . ' ON ' . $db->quoteName('x.parent') .' = '. $db->quoteName('y.id'))
  ->where($db->quoteName('y.name') .' = '. $db->quote($judgePick))
  ->andWhere(array($db->quoteName('x.parent').' = '. $db->quoteName('y.id'), $db->quoteName('x.active').' = 1'), $glue = 'AND')
  ->order($db->quoteName('x.name') . ' ASC');

// Reset the query using our newly populated query object.
$db->setQuery($query);

$result = $db->loadColumn();