简体   繁体   English

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

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

When parent = 0 means category当 parent = 0 表示类别

When parent = 1 means subcategory 1 connected to category 1 (id=1)当 parent = 1 表示子类别 1 连接到类别 1 (id=1)

When parent = 2 means subcategory 2 connected to category 2 (id=2)当 parent = 2 表示子类别 2 连接到类别 2 (id=2)

When parent... etc until 19 categories (id=19 with parent=0)当父...等直到 19 个类别(id=19,父=0)

What I need is to bring the names of subcategories in the sub-category form field according to user's choice in the category field.我需要的是根据用户在类别字段中的选择,将子类别表单字段中的子类别名称带入。 The category field works fine.类别字段工作正常。

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. This is the Query for the category field which works fine and gives us the user's choice ($judgePick) 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 2.这是子类别字段的查询试图解决

$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);
}

Assumption 1 - id to include ='.$db->quote($judgePick)假设 1 - 包含 id ='.$db->quote($judgePick)

Assumption 2 - For parent > 0 has to be equal to id of user's choice in Assumption 1假设 2 - 对于 parent > 0 必须等于用户在假设 1 中选择的 id

Expected result预期结果

Subcategory field to have the names ONLY according to user's choice in category field ($judgePick) where user's choice id equals parent.子类别字段的名称仅根据用户在类别字段 ($judgePick) 中的选择而定,其中用户的选择 ID 等于父级。 In other words, eg Arts & Entertainment is the category (parent=0) and has (id =1) and when the user chooses it in category form field, the subcategory form field should show all names with (parent=1)换句话说,例如 Arts & Entertainment 是类别 (parent=0) 并且具有 (id =1) 并且当用户在类别表单字段中选择它时,子类别表单字段应显示所有带有 (parent=1) 的名称

What you are looking for is probably a self join:您正在寻找的可能是自联接:

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

You can check the query on abstract sample here: http://www.sqlfiddle.com/#!9/ecc4bb/1/0您可以在此处查看对抽象示例的查询: http : //www.sqlfiddle.com/#!9/ecc4bb/1/0

Since with the input in your code you only get the name of the chosen category, thus we have to select its id too in the table, then we can find and select the subcategory's parent id and based on that, return the subcategories' names.由于在您的代码中输入您只能获得所选类别的名称,因此我们也必须在表中选择其 id ,然后我们可以找到并选择子类别的父 id,并基于此返回子类别的名称。

In Joomla syntax , your code and query should look like this:在 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();

那这个呢

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

暂无
暂无

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

相关问题 SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL) MySQL / PHP-选择表1中的列等于表2中的列的所有行,并创建一个foreach循环 - MySQL/PHP - Select all the rows where column from table 1 is equal to column from table 2 and creating a foreach loop PHP / MySQL-选择表1中的列等于表2中的列的所有行 - PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2 MYSQL:SELECT * FROM`user_log`其中`id`是连续的,并且`username` ==在多行中相等 - MYSQL: SELECT * FROM `user_log` WHERE `id` is sequential and `username` == EQUAL in multiple rows 返回mysql中单列总和等于“16”的行 - Return rows where sum of a single column is equal to “16” in mysql 选择MySQL行,而不考虑列的数据 - Select MySQL rows regardless of a column's data Mysql返回表的所有值,其中列等于用户输入 - Mysql Return all values of a table where column is equal to user input MySql获取id = xxx且id相同的行中其他表中的最新列大于一个的所有行 - MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one MySQL选择具有两个条件的行,并且具有相同列ID的计数大于1 - MySQL select rows with two where conditions and count greater than 1 with same column ID MySql获取列中每个ID的列表,其中搜索ID是一个数组 - MySql get list for each id in column where search id is an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM