Fero
Fero

Reputation: 13315

Need help with mysql query

  1. I am having a text-box which is auto complete.

  2. When i enter the name on the text-box the auto complete will work and provide the list from table.

  3. And when i click the save button the names which are entered in the text-box will inserted in a table. Until this, the process i working fine.

  4. My case is : Already existing users should not show in auto-complete.

Table Structure :

  1. Users Table

    uid, name

  2. work table

    wid, from_uid, to_uid,

How the mysql query should be.. Any help regarding this will be thankful and gratedful...

Upvotes: 0

Views: 44

Answers (1)

zerkms
zerkms

Reputation: 254886

select name
  from users
 where name like 'Jo%'
   and uid not in (select to_uid
                     from work
                    where from_uid = 666)

Supposing that user entered Jo substring and current user's id is 666

or another query that does the same (not sure which one will be more efficient in your particular case) but uses LEFT JOIN instead of subquery:

   select name
     from users u
left join work w on w.from_uid = 666
                and u.uid = w.to_uid
    where name like 'Jo%'
      and w.to_uid is null

Upvotes: 1

Related Questions