Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mysql select all with same data

Tags:

php

mysql

I have a table with 3 cols, incremeting id, name, and data

What i'm trying to achieve

you enter a $name... and returns $data...
then it takes $data and finds all $names that have the same $data.

SELECT * FROM table WHERE name ='$name' data='$data' and data!='0'

does not seem to cut it..

I'm basically trying to get all rows that have the same data as the entered $name's $data.

thanks in advance.

like image 212
hann Avatar asked Sep 21 '26 13:09

hann


2 Answers

SELECT * FROM table WHERE name ='$name' and data='$data' and data!='0'

You were missing an and

EDIT:

select * from table where
 name in 
   ( select data from table where name = '$name' )
like image 177
Eton B. Avatar answered Sep 24 '26 03:09

Eton B.


Try joining the table with itself, if you must do this in one query:

SELECT * FROM table t1,table t2 WHERE t1.name = '$name' AND t1.data=t2.data AND t1.name != t2.name

Alternately, use a nested query:

SELECT name FROM table WHERE data IN (SELECT data FROM table WHERE name='$name')

I recommend the nested query. It's easier to read.

What's wrong with your original query as posted is that you're missing an 'AND'. But it also won't do what you say you want it to.

like image 42
Borealid Avatar answered Sep 24 '26 02:09

Borealid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!