MySql Inner join of 2 table with WHERE Clauses - c#

I am making a query to 3 Mysql tables:
Sql = "SELECT " +
" u.iUserID, " +
" u.szPlayerName, " +
" u.szPlayerSurname, " +
" u.iPreferredPos, " +
" u.iAltPos, " +
" u.charType, " +
" c.szClubPrefix, "+
" tblteam.szTeamName " +
"FROM " +
" tblteam" +
" INNER JOIN tblusers u ON (tblteam.iTeamID = u.iTeamID) " +
" INNER JOIN tblclubs c ON (u.iClubID = c.iClubID) " +
"WHERE " +
"c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ' AND " +
"u.bActive = 1 " +
"AND u.charType= 'c'" +
"ORDER BY " +
" tblteam.szTeamName DESC, " +
" u.szPlayerSurname;";
my problem is that; this query only returns the data where it match c.szClubPrefix = 'CLM' and not where it match u.iClubID2 =' 2 ' please anyone can Help me what I am doing wrong here?
Thanks for your help.

Add parenthesis with respect to your preferences
"(c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ') AND " +
"u.bActive = 1 " +
"AND u.charType= 'c'"

wrap in parenthesis
(c.szClubPrefix = 'CLM' OR u.iClubID2 ='2')
and remove the whitespace before and after 2.
Note that dealing with numbers in MySQL you don't need the '
e.g.
iClubID2 = 2
instead of
iClubID2 ='2'

your where clause looks like this:
WHERE c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ' AND u.bActive = 1 AND u.charType= 'c'
and it equivalence:
WHERE c.szClubPrefix = 'CLM' OR ( u.iClubID2 =' 2 ' AND u.bActive = 1 AND u.charType= 'c' )
so, u.iClubID2 =' 2 ' is one of those 3 conditions must be all true.

Related

Using string SQL queries

I am getting the following error.
Message = "Incorrect syntax near the keyword 'AS'.\r\nIncorrect syntax near the keyword 'AS'."
I have the following sql string query.
private const string QueryString =
"SELECT DISTINCT "
+ " Person.[PersObjId]"
+ ",Person.[PtObjId]"
+ ",MSN.[PersObjId]"
+ ",MSN.[ExtrnId] AS '" + MSNumber + "'"
+ ",HlthProfMstrV.[RptName] AS '" + ItemName + "'"
+ ",HlthProfMstrV.[ItemStsId] AS '" + ItemId + "'"
+ "FROM [dbt1].[Person]"
+ "join"
+ " ( "
+ " SELECT "
+ " PersIdRptV.PersObjId"
+ " , PersIdRptV.ExtrnId"
+ " FROM"
+ " dbt1.PersIdRptV"
+ " join dbt1.IdIssMstrV on PersIdRptV.IdIssObjId = IdIssMstrV.IdIssObjId"
+ " WHERE"
+ " PersIdRptV.TypeId = 5"
+ " and"
+ " PersIdRptV.StpDate is NULL"
+ " ) AS MSN on dbt1.Person.PersObjId = MSN.PersObjId"
+ "left join dbt1.HlthProfMstrV on Person.PCPHlthProfObjId = HlthProfMstrV.HlthProfObjId"
+ " " + "WHERE" + " "
+ "Person.[ExpressId] =12"
+ " and" + " "
+ "Person.[StnTst] = 'Actv' "
+ " and" + " "
+ "MSN.[ExtrnId] = #MSN ";
I think this error is occuring when I am getting the first join result where I assign the result set to "MSN". I tried taking out the AS from AS MSN and I get another error saying the following:
Message = "Incorrect syntax near the keyword 'MSN'.\r\nIncorrect
syntax near the keyword 'MSN'."
I been looking at this for few hours. I am sure its something simple I am missing. Help appreciated!
You're missing a space between last column alias and From keyword:
+ ",HlthProfMstrV.[ItemStsId] AS '" + ItemId + "'"
+ " FROM [dbt1].[Person]"
Space missing before person and join
+ "FROM [dbt1].[Person]"
+ " join" --> Added space before join
+ "MSN.[ExtrnID] = '" + #MSN + "'"

How to update a specific column without disturbance in other columns in database?

("update Medicine set Name = ' " + textbox1.Text + " ', Company = ' " + textbox2.Text + " ', Type = ' " + textbox3.Text + " ', Quantity = ' " + textbox4.Text + " ' where P_id =' " + textbox5.Text + " ' ");
It does not working because whenever I update any column it makes other columns NULL.
One obvious solution is to construct the update with lists of columns.
An alternative is to use coalesce():
update Medicine
set Name = coalesce(' " + textbox1.Text + " ', Name),
Company = coalesce(' " + textbox2.Text + " ', Company),
Type = coalesce(' " + textbox3.Text + " ', Type,
Quantity = coalesce(' " + textbox4.Text + " ', Quantity)
where P_id =' " + textbox5.Text + " '
Actually, you should be using parameters rather than directly putting values in the query string. Putting parameters in directly makes the system vulnerable to SQL injection.
That said, I'm not sure how you are getting NULL values. You might really want:
update Medicine
set Name =(case when ' " + textbox1.Text + " ' = '' then '" + textbox1.Text + "' else Name end),
. . .

SQL C# WPF Query Expanded

I had a query answered here SQL Select All Without Values in Another Table.
I've just been asked to integrate the data from another database. This is what I have at the moment.
string _loanSubcontractor = TableNames.Default.LoansSubcontractors;
string _loanPacific = TableNames.Default.LoansPacific;
string _tools = TableNames.Default.Tools;
string _selectStatement = " SELECT [Tools].[Type], [Tools].[Brand], [Tools].[Serial], [Tools].[Year], [Tools].[Code] ";
string _groupBy = " GROUP BY [Tools].[Type], [Tools].[Brand], [Tools].[Serial], [Tools].[Year], [Tools].[Code], [Tools].[Working] ";
string _searchItems = " ([Tools].Code LIKE #toolSerial OR [Tools].Serial LIKE #toolSerial) AND ([Tools].[Working] = 'True' OR [Tools].[Working] IS NULL) ";
SqlConnection myConnection = new SqlConnection(Connection.Default.ConnectionString);
//Checks the main tool information
myConnection.Open();
SqlCommand getTool = new SqlCommand(
_selectStatement + "FROM [" + _tools + "] LEFT OUTER JOIN [" + _loanSubcontractor + "] ON " +
_tools + ".code = [" + _loanSubcontractor + "].ToolCode FULL JOIN [" + _loanPacific + "] ON " + _tools + ".Code = " +
_loanPacific + ".ToolCode WHERE [" + _loanSubcontractor + "].ToolCode IS NULL AND [" + _loanPacific + "].ToolCode IS NULL AND (" + _searchItems + ")" +
"UNION " +
_selectStatement + " FROM [" + _loanSubcontractor + "] INNER JOIN " + _tools + " ON " + _tools + ".Code = [" + _loanSubcontractor + "].ToolCode " +
"INNER JOIN " + _loanPacific + " ON " + _loanPacific + ".ToolCode = " + _tools + ".Code " + _groupBy +
"HAVING (COUNT(" + _loanSubcontractor + ".ReturnDate) = COUNT(*) OR COUNT(" + _loanPacific + ".ReturnDate) = COUNT(*)) " +
" AND " + _searchItems, myConnection);
getTool.Parameters.Add("#toolSerial", SqlDbType.NVarChar).Value = "%" + toolSerial + "%";
What I have is two loan tables (one for employees and one for subcontractors) because the attribute names are different and the data types are also different. Essentially, I need to check that the tool is working, and the tool is not hired out in either of the loan tables (as shown by the return date being null). There may or may not be a loan in either of the tables.
Also, could someone provide me with a link that shows good formatting techniques for SQL within C#?
i sagest you use linq to SQL . this is a part of ADO.NET and you can use long query with 100 % acres and easy way
msdn.microsoft.com for linq to SQL
LINQ to SQL: .NET Language-Integrated Query for Relational Data
Simple LINQ to SQL in C#

What is wrong with this custom query syntax used inside C# code?

The query looks like this:
string query = "IF NOT EXISTS(
SELECT * from clienti
where CodCliente= ' " + id + " '
AND NomeCliente= ' " + emri + " '
AND RagioneSociale=' " + ragSoc + " '
AND PartitaIVA=' " + piva + " ')
Insert INTO clienti VALUES(
' " + id +
" ',' " + emri +
" ',' " + ragSoc +
" ',' " + piva + " ')
else
UPDATE clienti
SET(' " + id + " ',' " + emri + " ',' " + ragSoc + " ',' " + piva + " ')";
I just keep getting the same problem stated below:
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 'IF NOT EXISTS(SELECT * from clienti where CodCliente= ' 1 ' AND NomeCliente= ' ' at line 1
I think you're missing the THEN and END IF keywords. Check mysql manual
IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF
You are concatenating blank spaces before and after all your variables. Also, make sure all your variables are in string format.

How can you use parameterized statements with DB2 Text Search?

I've tried this:
select * from ourschema.mytable
where contains(mysearchablefield, #searchTerms) = 1;
Where #searchTerms was set to "search terms"
Unfortunately, it only produced an error:
ERROR [42610] [IBM][DB2/NT] SQL0418N A statement contains a use of a parameter marker that is not valid. SQLSTATE=42610
Is there a way to use parameterized queries for text search with DB2? If not, is there a document which describes the syntax in detail for manual (ugh) escaping of the search terms (quotes, etc)?
Instead of #field you need to use "?". Everything is basically the same.
Okay, here is a live code sample.
sqlStmt = "SELECT COMPLAINT_NUMBER, VIOLATION_NUMBER, COMMON_ADDRESS_KEY, " +
"DEPT_CODE, DEPT_CODE_DESC, DIVISION_CODE, DIVISION_CODE_DESC, " +
"EMPLOYEE_NAME, COMPLAINT_CODE, COMPLAINT_CODE_DESC, COMPLAINT_DATE, " +
"COMMON_ADDRESS_OWNER, RESOLUTION_CODE, 1 AS SORTORDER " +
"FROM QMFILES/NVMASTP " +
"WHERE VCLOSEDATE = 0 AND " +
"DEPT_CODE LIKE #DEPT_CODE1 AND " +
"DIVISION_CODE LIKE #DIVISION_CODE1 AND " +
"COMPLAINT_DATE BETWEEN #FROM_COMPLAINT_DATE1 AND #TO_COMPLAINT_DATE1 " +
statusQry +
"UNION " +
"SELECT COMPLAINT_NUMBER, VIOLATION_NUMBER, COMMON_ADDRESS_KEY, " +
"DEPT_CODE, DEPT_CODE_DESC, DIVISION_CODE, DIVISION_CODE_DESC, " +
"EMPLOYEE_NAME, COMPLAINT_CODE, COMPLAINT_CODE_DESC, COMPLAINT_DATE, " +
"COMMON_ADDRESS_OWNER, RESOLUTION_CODE, 2 AS SORTORDER " +
"FROM QMFILES/NVMASTP " +
"WHERE VCLOSEDATE <> 0 AND " +
"DEPT_CODE LIKE #DEPT_CODE2 AND " +
"DIVISION_CODE LIKE #DIVISION_CODE2 AND " +
"COMPLAINT_DATE BETWEEN #FROM_COMPLAINT_DATE2 AND #TO_COMPLAINT_DATE2 " +
statusQry +
"ORDER BY DEPT_CODE, DIVISION_CODE, COMPLAINT_CODE, SORTORDER";
iDB2Command cmd = new iDB2Command(sqlStmt, conn);
conn.Open();
cmd.DeriveParameters();
conn.Close();
cmd.Parameters["#DEPT_CODE1"].Value = dept;
cmd.Parameters["#DIVISION_CODE1"].Value = serviceArea;
cmd.Parameters["#DEPT_CODE2"].Value = dept;
cmd.Parameters["#DIVISION_CODE2"].Value = serviceArea;
cmd.Parameters["#FROM_COMPLAINT_DATE1"].Value = Convert.ToDecimal(fromDateString);
cmd.Parameters["#TO_COMPLAINT_DATE1"].Value = Convert.ToDecimal(toDateString);
cmd.Parameters["#FROM_COMPLAINT_DATE2"].Value = Convert.ToDecimal(fromDateString);
cmd.Parameters["#TO_COMPLAINT_DATE2"].Value = Convert.ToDecimal(toDateString);
I hope this helps you out more.

Categories

Resources