简体   繁体   English

如何使用sql查询显示特定类别下的所有数据?

[英]How can I show all data under specific category using sql query?

well, I have 2 Mysql table which structure is bellow : 好吧,我有2个Mysql表,结构如下:

Table Jobs 工作

job_id---job_cat_id---job_title---job_description---job_data----is_active
=========================================================================
1        1            title 1     description 1     2016-05-06  1
2        2            title 2     description 2     2016-05-06  0
3        2            title 3     description 3     2016-05-06  1

Table job_details 表job_details

job_cat_id---job_cat_name
=========================
1            cat name 1
2            cat name 2
3            cat name 3

Now I want to show all jobs under each category from jobs table. 现在我想在jobs表中显示每个类别下的所有作业。 Eg 例如

What I need to show : 我需要展示的内容:

Job Category 1
    1. job 1 from category 1
    2. Job 2 from category 1
Job Category 2
    1. Job 3 from category 2

So to do this I am using following sql query but can't get the correct result : 所以要做到这一点我使用以下sql查询但无法获得正确的结果:

$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, job_category.job_cat_name FROM jobs LEFT JOIN job_category ON job_category.job_cat_id = jobs.job_cat_id WHERE jobs.is_active = '1' ");        

while($result = mysqli_fetch_array($get_job) ) {

    $job_id = (int) $result['job_id'];
    $job_title = htmlspecialchars($result['job_title']);
    $job_category = htmlspecialchars($result['job_cat_name']);   

    echo "<h4>$job_category</h4>";  
    echo "<p>$job_title</p>";           
}   

Now, It's showing me all category with all jobs but I want to show all jobs under each category. 现在,它向我显示所有工作的所有类别,但我想显示每个类别下的所有工作。

What is showing now : 现在在演什么 :

Job Category 1
    1. job 1 from category 1        
Job Category 1
    1. Job 2 from category 1
Job Category 2
    1. Job 3 from category 2

First we have to remember that the result from a SELECT query is a newly generated table. 首先,我们必须记住SELECT查询的结果是新生成的表。 It is not a multi dimensional array. 它不是一个多维数组。 If it were a multidimensional array, then you could get away with printing the job category at the beginning of each new array which could be grouping up all the jobs in a single category, however since this is not the type of result obtained by the SQL SELECT QUERY, you are printing the job category after each line: 如果它是一个多维数组,那么您可以在每个新数组的开头打印作业类别,这可能会将所有作业分组到一个类别中,但是因为这不是SQL获得的结果类型SELECT QUERY,您在每行后打印作业类别:

echo "<h4>$job_category</h4>";  
echo "<p>$job_title</p>";     

Solution: 解:

A solution to your problem would be to first use the ORBER BY ASC in your sql query: 解决您的问题的方法是首先在SQL查询中使用ORBER BY ASC

$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, job_category.job_cat_name FROM jobs LEFT JOIN job_category ON job_category.job_cat_id = jobs.job_cat_id WHERE jobs.is_active = '1' ORDER BY job_cat_id ASC");

From there, you know that the jobs in each category should at least be grouped up next to each other (from lowest to highest like 1,1,1,1,2,2,3,3,3). 从那里,你知道每个类别的工作至少应该彼此相邻(从最低到最高,如1,1,1,1,2,2,3,3,3)。 What you can now do is have a conditional print the $job_category if AND ONLY IF it hasn't been printed already previously. 你现在可以做的是有一个条件打印$job_category ,如果它只是以前没有打印过。

Change this line: 改变这一行:

echo "<h4>$job_category</h4>";

into this line: 进入这一行:

if ($previous_print != $job_category)
{
  echo "<h4>$job_category</h4>";  
  $previous_print = $job_category;
}

Let me know if it works now. 如果现在有效,请告诉我。

另一种解决方案可能是,如果您只使用group by job_cat_id运行一个查询,然后在内部循环中编写另一个查询以使用where子句job_cat_id获得所需结果。

You need to do 2 queries here. 你需要在这里做2个查询。 Here's an exmaple code, might need some tweaks acording to your table column names: 这是一个exmaple代码,可能需要根据您的表列名称进行一些调整:

<?php

$query = "SELECT `job_cat_id`, `job_cat_name`, COUNT(`jobs`.`id`) as `jobs`
          FROM `job_category`
          GROUP BY `job_cat_id`";
$get_cat = mysqli_query($conn, $query);

$cats = [];
while($result = mysqli_fetch_array($get_cat) ) {
    $result['jobs'] = [];
    $cats[$result['job_cat_id']] = $result;
}

$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, jobs.job_cat_id FROM jobs WHERE jobs.is_active = '1' AND `job_cat_id` IN (" . implode(',', array_keys($cats)) . ")");

while($result = mysqli_fetch_array($get_job) ) {
    $cats[$result['job_cat_id']][] = $result;
}

foreach ($cats as $cat) {
    $job_category = htmlspecialchars($cat['job_cat_name']);
    echo "<h4>$job_category</h4>";

    foreach ($cat['jobs'] as $job) {
        $job_title = htmlspecialchars($job['job_title']);
        echo "<p>$job_title</p>";
    }
}

暂无
暂无

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

相关问题 如何使用SQL选择查询获取特定数据? - How can I get a specific data using SQL select query? 如何显示所有帖子,但在wordpress上排除特定类别? - How do I show all posts but exclude a specific category on wordpress? 在Wordpress中显示特定父类别的子类别下的所有帖子标题 - Show all post titles under the subcategories of a specific parent category in Wordpress 如何在 WooCommerce 中显示特定产品类别的正常价格 - How can I show regular price for a specific product category in WooCommerce 当该类别下的所有产品都缺货时,如何在 woocommerce 类别列表菜单上隐藏产品类别? - How can i hide a product category on woocommerce category list menu when all the products under this category are out of stock? 如何在WordPress查询中获取分类法类别的所有记录? - How can I get all records of taxonomy category in WordPress Query? 在 laravel 中编辑类别时,如何显示特定类别的先前值? - how can i show the previous value of a specific category while editing a category in laravel? 如何在php中使用每个邮件显示所有数据? - how can I show all data in a mail using for each in php? 如何在Woocommerce的“简短描述”区域下为特定类别的项目输出静态文本? - How can I output static text under the “Short Description” area in Woocommerce for items in a specific category? 如何在MVC(AngularJS,HTML,PHP)中显示来自SQL数据库的特定数据 - How can I show specific data from SQL data base in MVC (AngularJS, HTML, PHP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM