3

I want to filtering data from SQL to my datagrid.

I have :

1 table (tableX)

3 Columns

 Col1    Col2      Col3
1/x/10    BJB    1/20/20
1/y/10    BJB    1/20/30
1/x/10    BJB    1/20/30
1/y/10    BJB    1/20/20

2 datagrid (dg1, dg2)

i want insert :

dg1 with col1 "1/x/10" and col3 "10/20/20"

dg2 with col1 "1/y/10 and col3 "10/20/20

i can filter with only one Col3

"SELECT Col1, Col2, Col3  FROM Tablex WHERE Col3='10/20/20'"

how to filter col1 witch contain "x" or "y" and col3?

==========================================================================

O YEAH.. thanks for the answer.

this for dg1

("SELECT Col1, Col2, Col3  FROM TableX WHERE Col1 like ('%/X/%') AND Col3='10/20/20'")

and this for dg2, the different is just X and Y..lol

("SELECT Col1, Col2, Col3  FROM TableX WHERE Col1 like ('%/Y/%') AND Col3='10/20/20'")
6
  • 1
    If I am reading your question correctly this SO post will help you. stackoverflow.com/questions/5843537/… Commented Apr 14, 2014 at 8:39
  • Are you saying you want to make one call to the database that will fill both datagrids? Commented Apr 14, 2014 at 8:59
  • no, iam not.. 1 command for 1 datagrid. Commented Apr 14, 2014 at 9:03
  • Then SELECT Col1, Col2, Col3 FROM Tablex WHERE Col3='10/20/20' AND Col1 = '1/x/10' for one and SELECT Col1, Col2, Col3 FROM Tablex WHERE Col3='10/20/20' AND Col1 = '1/y/10' for the other. Commented Apr 14, 2014 at 9:05
  • @Fred your code is working but.. col1 is not always "1/x/10" and "1/y/10.. the x and y is static, but 1 and 10 always change.. sory my bad question... Commented Apr 14, 2014 at 9:10

2 Answers 2

1
SELECT Col1, Col2, Col3  
FROM Tablex 
WHERE Col3='10/20/20'
AND (COL1 like '%/x/%' or COL1 like '%/y/%')
Sign up to request clarification or add additional context in comments.

4 Comments

ihave error ERROR [42000] [MySQL][ODBC 5.2(w) Driver][mysqld-5.5.25a]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%'X'%)' at line 1.
Why use Like? Where (COL1 = '1/x/10' or COL1 like '1/y/10') is more effcient but based on the dataset above this query is the same as SELECT Col1, Col2, Col3 FROM Tablex
@Fred Are you sure that everytime there will be only these strings ('1/x/10', '1/y/10')? OP ask how to filter col1 witch contain "x" or "y" and I think this is only example data.
Apologies, the question was obviously clearer to you than me! :) ps I didn't downvote you.
0
SELECT Col1, Col2, Col3  
FROM Tablex 
WHERE COL1 IN ('1/x/10', '1/y/10')
AND Col3 = '10/20/20

Or you can make the COL1 filter more ambiguous.

SELECT Col1, Col2, Col3  
FROM Tablex 
WHERE COL1 IN ('%/x/%', '%/y/%')
AND Col3 = '10/20/20

2 Comments

SELECT Col1, Col2, Col3 FROM Tablex WHERE COL1 IN ('x', 'y') AND Col3 = '10/20/20' its return nothing..
Have a look now, updated the answer, i was originally just using x and y as placeholders, i didn't realise you have them in your data.

Your Answer

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