0

Hello I'm having a problem.

I want to search values in the name column using the where clause. Example table is this with values stored.

table1
--------------------------
id  | name   | anotherid
--------------------------
1   | name1  | 1
2   | name2  | 1
2   | name2  | 2

I have textboxes that I can change how many they appear. For example I have two textboxes. The value on textbox one is 'name1' and the second is 'name2'

My query must find those two values in table1 and, when they're found, their anotherid column must be the same also.

From the first table, All I want to get is this:

table1
--------------------------
id  | name   | anotherid
--------------------------
1   | name1  | 1
2   | name2  | 1

However when a situation like this appears and I still have two textboxes with the same values:

table1
--------------------------
id  | name   | anotherid
--------------------------
1   | name1  | 1
2   | name2  | 1
2   | name2  | 2
3   | name3  | 1

The result must be empty.

Now back to the first table. I used this code.

SELECT * FROM `table1` WHERE name = "name1" and name = "name2"
and
SELECT * FROM `table1` WHERE name = "name1" and name = "name2" group by anotherid

but all I get are empty results.

How do you do this?

3 Answers 3

1

The problem is in you WHERE clause,

WHERE name = "name1" and name = "name2" will always return false. Use OR instead.

Try any of these:

1. Selecting with name1,name2 and anotherid=1

SELECT * 
FROM table1 
WHERE (name="name1" OR name="name2") 
AND anotherid=1

The result will be:

id   name    anotherid  
1    name1   1
2    name2   1

2. Grouping by anotherid:

SELECT * FROM table1 
GROUP BY anotherid

Result:

ID  NAME    ANOTHERID
1   name1   1
2   name2   2

3. Grouping by name:

SELECT * FROM table1
GROUP BY name

Result:

ID  NAME    ANOTHERID
1   name1   1
2   name2   1
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry. I edited my question. you cannot have anotherid be an input.
Then what you mean by GROUP BY anotherid, my friend?
I was confused when the first line didn't work so I tried many things. I'm kind of new in this.
@user3436844: Edited my answer. Pick what kind of result you are looking for.
#1 is good but anotherid isn't an input #2 - #3 I edited my question again to give more information
|
0
 SELECT * FROM `table1` WHERE name = "name1" OR name = "name2"  group by anotherid 

That should do.

1 Comment

Sorry. I edited my question. you cannot have anotherid be an input.
0

Try this

SELECT * FROM `table1` group by name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.