1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Reverse IN()-function in MySQL?

Discussion in 'MySQL' started by ChrisPhp, Feb 21, 2008.

  1. #1
    Hi!
    I am creating a website where multiple persons can work on the same story.
    I have this table:
    id | authors
    1 jim,steward
    2 jim
    3 george,steward
    etc.

    It's a list of stories and who worked on it. Say I want to know what stories Jim worked on.
    I know the IN()-function. You can search like: ...where authors IN('jim'). But I want a "reverse-function". So the field name is inside the brackets.
    Something like ....where 'jim' IN(authors). This doesn't work, but is there anything like that?
     
    ChrisPhp, Feb 21, 2008 IP
  2. flippers.be

    flippers.be Peon

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    change your db design..

    make a table authors
    make a table stories
    make a table author_stories that has 2 columns, authorid and storyid which is the link between both tables

    that way you can easily search who worked on what story, or on what stories an author worked
     
    flippers.be, Feb 21, 2008 IP
  3. 00johnny

    00johnny Peon

    Messages:
    149
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    good solution flippers, thats probably the best way to do it
    but if your data is in a list "joe, bob, jim" and you don't want to change your tables you can always use wild cards
    where authors like '%jim%'
     
    00johnny, Mar 5, 2008 IP
  4. JackDuluoz

    JackDuluoz Peon

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The first posted solution is the right one.
    As for using wildcards %% I wouldn't use it because '%jim%' does also match 'jimmy' so you would not get accurate results :)
     
    JackDuluoz, Mar 6, 2008 IP