2个表上的sql排序相同的列名

时间:2016-01-19 10:12:16

标签: mysql sorting

我有两张桌子:

  1. projects_tasks
  2. projects_tasks_deadlines
  3. 现在我有一个简单的查询,我在截止日期前对每个任务进行排序。起初任务只能有1个截止日期。

    然后我被要求能够在一个任务上设置多个截止日期。这就是为什么我有第二张表projects_tasks_deadlines的原因。

    我设法允许用户选择应激活哪个截止日期(任务明细)。但是在包含所有任务的列表中,我不知道如何在两个表之间进行排序,如果这至少是可能的。

    这是我目前正在使用的查询:

    SELECT 
        projects_tasks.id AS task_id,
        projects_tasks.name AS task_name,
        projects_tasks.deadline AS task_deadline,
        projects_tasks.created_by AS task_creator,
        labels.name AS task_label,
        users.name AS task_responsible_name,
        users.surname AS task_responsible_surname,
        users2.name AS task_assigner_name,
        users2.surname AS task_assigner_surname
    FROM `projects_tasks`
        INNER JOIN `labels` ON labels.id = projects_tasks.label_id
        INNER JOIN `users` ON users.id = projects_tasks.responsible_id
        INNER JOIN `users` AS `users2` ON users2.id = projects_tasks.created_by
    WHERE projects_tasks.project_id = :project_id
    AND labels.id != '8'
    ORDER BY -projects_tasks.deadline DESC
    

    我如何实现我的目标,检查第二个表中的截止日期是否有效,并使用相应的`task_id。如果找到匹配,请将该截止日期计入计数并排序..

    一个例子:

    表projects_tasks:

    id | name | deadline
    --------------------
    1  | test | 2016-01-19
    2  | test2| 2016-02-15
    3  | test | 2016-01-25
    

    表projects_tasks_deadlines

    id | task_id | deadline  | active
    --------------------------------
    1  | 1       | 2016-01-20| 1 // yes
    2  | 1       | 2016-01-24| 0 // no
    3  | 2       | 2016-02-25| 0
    

    结果应为

    1. 2016-01-20 - > projects_tasks_deadlines
    2. 2016-01-25 - > projects_tasks
    3. 2016-02-15 - > projects_tasks
    4. 感谢您的帮助,希望这很清楚......

      **更新**

      SELECT 
          projects_tasks.id AS task_id,
          projects_tasks.name AS task_name,
          projects_tasks.created_by AS task_creator,
          projects_tasks_deadlines.id AS deadline_id,
          labels.name AS task_label,
          users.name AS task_responsible_name,
          users.surname AS task_responsible_surname,
          users2.name AS task_assigner_name,
          users2.surname AS task_assigner_surname,
          CASE WHEN projects_tasks_deadlines.deadline_active = '1' THEN projects_tasks_deadlines.deadline AS task_deadline ELSE
          projects_tasks.deadline AS task_deadline END
      FROM `projects_tasks`
          INNER JOIN `labels` ON labels.id = projects_tasks.label_id
          INNER JOIN `users` ON users.id = projects_tasks.responsible_id
          INNER JOIN `users` AS `users2` ON users2.id = projects_tasks.created_by
          LEFT JOIN projects_tasks_deadlines ON projects_tasks_deadlines.task_id = projects_tasks.id
      WHERE projects_tasks.project_id = '1'
      AND labels.id != '8'
      ORDER BY CASE WHEN projects_tasks_deadlines.deadline_active = 1 THEN -projects_tasks_deadlines.deadline ELSE -projects_tasks.deadline END DESC
      

      我应该如何处理CASE中的SELECT

2 个答案:

答案 0 :(得分:2)

您可以通过执行条件订单。如果active = 1,则使用projects_tasks_deadline表中的截止日期,否则使用projects_tasks表。所以像这样:

SELECT 
    projects_tasks.id AS task_id,
    projects_tasks.name AS task_name,
    projects_tasks.deadline AS task_deadline,
    projects_tasks.created_by AS task_creator,
    labels.name AS task_label,
    users.name AS task_responsible_name,
    users.surname AS task_responsible_surname,
    users2.name AS task_assigner_name,
    users2.surname AS task_assigner_surname
FROM `projects_tasks` 
    INNER JOIN `labels` ON labels.id = projects_tasks.label_id
    INNER JOIN `users` ON users.id = projects_tasks.responsible_id
    INNER JOIN `users` AS `users2` ON users2.id = projects_tasks.created_by
LEFT JOIN projects_tasks_deadline ptd ON id = task_id
WHERE projects_tasks.project_id = :project_id
AND labels.id != '8'
ORDER BY CASE WHEN ptd.active = 1 THEN ptd.deadline ELSE projects_tasks.deadline END DESC

希望有道理吗?

答案 1 :(得分:0)

我最终找到了解决方案:

CASE

我错过了SELECT中的另一个(CASE WHEN projects_tasks_deadlines.deadline_active = '1' THEN projects_tasks_deadlines.deadline ELSE projects_tasks.deadline END) AS task_deadline

`<preference name="SplashScreen" value="screen" />
<preference name="SplashScreenDelay" value="10000" />

<platform name="android">
    <splash src="res/screens/android/320X470.jpg" density="land-hdpi" />
    <splash src="res/screens/android/320X470.jpg" density="land-ldpi" />
    <splash src="res/screens/android/320X470.jpg" density="land-mdpi" />
    <splash src="res/screens/android/320X470.jpg" density="land-xhdpi" />
    <splash src="res/screens/android/320X470.jpg" density="port-hdpi" />
    <splash src="res/screens/android/320X470.jpg" density="port-ldpi" />
    <splash src="res/screens/android/320X470.jpg" density="port-mdpi" />
    <splash src="res/screens/android/320X470.jpg" density="port-xhdpi" />
  </platform>

 <vs:plugin name="cordova-plugin-splashscreen" version="3.0.0" />`

这过滤了正确的截止日期,他们也在正确的截止日期前排序。