www.webdeveloper.com
Results 1 to 3 of 3

Thread: querying multiple values in one cell

  1. #1
    Join Date
    Dec 2010
    Posts
    25

    Smile querying multiple values in one cell

    Hey all,

    Is it possible to do the following:

    If I have a column called 'fruit' in a table called 'mytable' which contains cells such as:

    apple
    orange
    apple orange
    apple orange orange

    ...can I query this so that

    SELECT * FROM mytable WHERE fruit='apple' && 'apple'

    would return nothing, since there are no cells with the word 'apple' twice,

    whereas

    SELECT * FROM mytable WHERE fruit='orange' && 'orange'

    would return the third cell, because 'orange' DOES appear twice?

    If this question doesn't make sense to you, you're probably not best-suited to respond :-)

  2. #2
    Join Date
    Dec 2010
    Location
    Knoxville, Tn
    Posts
    22
    With MySQL, you can query for a match on two separate words like this
    Code:
    SELECT *
    FROM `mytable`
    WHERE `fruit` LIKE '%apple%' AND `fruit` LIKE '%orange%'
    but if you search for '%apple%' AND '%apple%', SQL doesn't realize you are looking for two instances of the same word. To do that, you would need to format your query like this
    Code:
    SELECT *
    FROM `mytable`
    WHERE `name` LIKE '%apple%apple%'
    Hope that helps.

  3. #3
    Join Date
    Dec 2010
    Location
    Knoxville, Tn
    Posts
    22
    I forgot to change my column name in that last snippet- sorry. It would be this:
    Code:
    SELECT *
    FROM `mytable`
    WHERE `fruit` LIKE '%apple%apple%'

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles