Solved

MySQL query to show different levels from a table.

Posted on 2016-10-30
5
38 Views
Last Modified: 2016-11-14
I have a database which is for task management.
The main table currently shows all jobs by newest first.
What I want to do is show (for our techs) is the Tech signed in at the top with his open jobs then the any staff categorised jobs underneath.

Something like:
Task date, Task Priority, Customer, Assigned to (tech logged in),
*
*
*
*
These jobs will be listed in date order decending for that tech, then under that it will show jobs assigned in date order decending for anyone.
Task date, Task Priority, Customer, Assigned to Anyone,

The current query is:
SELECT * FROM database.Tasks WHERE `Complete` = '0' ORDER BY `EntryDate` DESC

Open in new window

The Complete=0 just means that jobs are currently open.
Fields involved would be similar to `TechID`='4'

I hope this makes sense.
0
Comment
Question by:kiwistag
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
Comment Utility
Something like this should do it, assuming that the current tech's id is stored (sanitised, with single quotes escaped) in variable $current_user:
<?php $sql = "SELECT *, IF( assigned_to = '$current_user', 1, 0 ) AS current_tech
FROM database.Tasks 
WHERE `Complete` = '0' 
ORDER BY current_tech DESC,`EntryDate` DESC";

Open in new window

0
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 total points
Comment Utility
I think you can also use case..try.

--

  SELECT *, CASE `assigned_to` 
  WHEN 'variable_Value_you_want_to_check' THEN 1  
  ELSE 0
  END as 'current_tech_user'
  FROM database.Tasks WHERE `Complete` = '0' ORDER BY `EntryDate` DESC
 
--

Open in new window

0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Hi kiwistag,
Any feedback on this?

Regards,
Pawan
0
 
LVL 6

Author Comment

by:kiwistag
Comment Utility
Hi Pawan.
Have been away. Trying out the suggestions today.
Cheers
0
 
LVL 6

Author Closing Comment

by:kiwistag
Comment Utility
Terry's solution worked best in my case. Thank you both for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now