I have an issue with SQL IN query I'm storing multiple employees id in a table separated with comma...











up vote
0
down vote

favorite













This question already has an answer here:




  • Is storing a delimited list in a database column really that bad?

    10 answers




I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.



My query is:



select 
t.*,
e.emp_name,
d.department_name
from
task as t
LEFT JOIN employee as e on(e.emp_id=t.assign_to)
LEFT JOIN department as d on(d.depart_id=e.depart_id)
where
t.task_status='PENDING'
AND t.created_by!='31'
AND t.assign_to IN ('31')
order by
t.task_id DESC


The stored value in database










share|improve this question















marked as duplicate by Madhur Bhaiya, u_mulder php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 6:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • Is storing a delimited list in a database column really that bad?

      10 answers




    I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.



    My query is:



    select 
    t.*,
    e.emp_name,
    d.department_name
    from
    task as t
    LEFT JOIN employee as e on(e.emp_id=t.assign_to)
    LEFT JOIN department as d on(d.depart_id=e.depart_id)
    where
    t.task_status='PENDING'
    AND t.created_by!='31'
    AND t.assign_to IN ('31')
    order by
    t.task_id DESC


    The stored value in database










    share|improve this question















    marked as duplicate by Madhur Bhaiya, u_mulder php
    Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 12 at 6:55


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • Is storing a delimited list in a database column really that bad?

        10 answers




      I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.



      My query is:



      select 
      t.*,
      e.emp_name,
      d.department_name
      from
      task as t
      LEFT JOIN employee as e on(e.emp_id=t.assign_to)
      LEFT JOIN department as d on(d.depart_id=e.depart_id)
      where
      t.task_status='PENDING'
      AND t.created_by!='31'
      AND t.assign_to IN ('31')
      order by
      t.task_id DESC


      The stored value in database










      share|improve this question
















      This question already has an answer here:




      • Is storing a delimited list in a database column really that bad?

        10 answers




      I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.



      My query is:



      select 
      t.*,
      e.emp_name,
      d.department_name
      from
      task as t
      LEFT JOIN employee as e on(e.emp_id=t.assign_to)
      LEFT JOIN department as d on(d.depart_id=e.depart_id)
      where
      t.task_status='PENDING'
      AND t.created_by!='31'
      AND t.assign_to IN ('31')
      order by
      t.task_id DESC


      The stored value in database





      This question already has an answer here:




      • Is storing a delimited list in a database column really that bad?

        10 answers








      php mysql sql phpmyadmin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 8:20









      Joe McMahon

      2,1871225




      2,1871225










      asked Nov 12 at 6:48









      Madhukant Tiwari

      1




      1




      marked as duplicate by Madhur Bhaiya, u_mulder php
      Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 12 at 6:55


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Madhur Bhaiya, u_mulder php
      Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 12 at 6:55


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          IN doesn't work like that



          Example if your data looks like:



          ManagerName, ManagerOf
          'John', 'Steve,Sarah,Dave'


          You can't do:



          SELECT * FROM managers WHERE 'sarah' IN ManagerOf


          IN is best conceived as an extension of OR:



          SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
          --is the same as:
          SELECT * FROM managers WHERE
          managerof = 'Sarah' OR
          managerof = 'Steve'


          There would be as many OR clauses as there are items in the IN list.



          Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then



          There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest



          You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query






          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            IN doesn't work like that



            Example if your data looks like:



            ManagerName, ManagerOf
            'John', 'Steve,Sarah,Dave'


            You can't do:



            SELECT * FROM managers WHERE 'sarah' IN ManagerOf


            IN is best conceived as an extension of OR:



            SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
            --is the same as:
            SELECT * FROM managers WHERE
            managerof = 'Sarah' OR
            managerof = 'Steve'


            There would be as many OR clauses as there are items in the IN list.



            Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then



            There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest



            You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query






            share|improve this answer



























              up vote
              1
              down vote













              IN doesn't work like that



              Example if your data looks like:



              ManagerName, ManagerOf
              'John', 'Steve,Sarah,Dave'


              You can't do:



              SELECT * FROM managers WHERE 'sarah' IN ManagerOf


              IN is best conceived as an extension of OR:



              SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
              --is the same as:
              SELECT * FROM managers WHERE
              managerof = 'Sarah' OR
              managerof = 'Steve'


              There would be as many OR clauses as there are items in the IN list.



              Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then



              There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest



              You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                IN doesn't work like that



                Example if your data looks like:



                ManagerName, ManagerOf
                'John', 'Steve,Sarah,Dave'


                You can't do:



                SELECT * FROM managers WHERE 'sarah' IN ManagerOf


                IN is best conceived as an extension of OR:



                SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
                --is the same as:
                SELECT * FROM managers WHERE
                managerof = 'Sarah' OR
                managerof = 'Steve'


                There would be as many OR clauses as there are items in the IN list.



                Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then



                There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest



                You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query






                share|improve this answer














                IN doesn't work like that



                Example if your data looks like:



                ManagerName, ManagerOf
                'John', 'Steve,Sarah,Dave'


                You can't do:



                SELECT * FROM managers WHERE 'sarah' IN ManagerOf


                IN is best conceived as an extension of OR:



                SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
                --is the same as:
                SELECT * FROM managers WHERE
                managerof = 'Sarah' OR
                managerof = 'Steve'


                There would be as many OR clauses as there are items in the IN list.



                Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then



                There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest



                You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 7:54

























                answered Nov 12 at 7:41









                Caius Jard

                9,01611136




                9,01611136















                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)