Jump to content

multiple columns with multiple LIKE


blob

Recommended Posts

my table 'Answers' is:

 

-----------------

key  |  value

==========

nam  |  John

-----------------

ema  |  john@aol.com

-----------------

age  |  44

-----------------

pho  |  212

-----------------

web  |  aol.com

-----------------

add  |  12 high

-----------------

 

 

 

i would like to select 4 values (nam,ema,pho,web) and ignore the other values

and place them into array like this

 

info[0] = 'John'

info[1] = 'john@aol.com'

info[2] = '212'

info[3] = 'aol.com'

 

i started with this statement

 

SELECT

t1.value

FROM Answers t1

WHERE t1.key LIKE 'nam'

 

but how do i add another column?

 

this doesn't seem to work:

 

SELECT

t1.value, t2.value

FROM Answers t1,t2

WHERE t1.key LIKE 'nam' OR t2.key LIKE 'ema'

Link to comment
Share on other sites

the nam, ema, pho, web are values in the column 'key' so i can't select them directly but changing OR to AND seem to have worked

 

 

SELECT

t1.value, t2.value

FROM t1,t2

WHERE t1.key LIKE 'nam' AND t2.key LIKE 'ema'

 

and mixing in id column with another table this worked also

 

SELECT

g.id, t1.value, t2.value, t3.value, t4.value

FROM g, t1, t2, t3, t4

WHERE

g.id = t1.id AND

g.id = t2.id AND

g.id = t3.id AND

g.id = t4.id AND

t1.key LIKE 'nam' AND

t2.key LIKE 'ema' AND

t3.key LIKE 'pho' AND

t4.key LIKE 'web'

 

 

 

 

 

Link to comment
Share on other sites

i think it's obvious i am not familiar with JOIN :)

 

i am trying to get an array with multiple records, each containing values for 4 matching keys

 

also a very smart person pointed out to me that in my instance i can use = instead of LIKE, as i am matching exact string.

 

so here is my try with JOIN, would this be a correct way of doing select?

 

SELECT

g.id, t1.value, t2.value, t3.value, t4.value

FROM g

JOIN t1 on g.id = t1.id and t1.key = 'nam'

JOIN t2 on g.id = t2.id and t2.key = 'ema'

JOIN t3 on g.id = t3.id and t3.key = 'pho'

JOIN t4 on g.id = t4.id and t4.key = 'web'

 

i appreciate very much your help and time to teach me!

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.