Reputation: 157
I am using INNERJOIN query on two tables naming EMPLOYEE & DEPARTMENT. There exists a common empID(1234) which is common in both the tables and I want to fetch the data from both tables using join query. BUt its not working. Here is my code below:
NSString *querySQL = [NSString stringWithFormat:
@"Select EMPLOYEE.* from EMPLOYEE INNER JOIN DEPARTMENT ON EMPLOYEE.empID = DEPARTMENT.empID =%@",_txtfind.text];
NSLog(@"fetch query is%@",querySQL);
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:name];
NSString *department = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
[resultArray addObject:department];
NSString *year = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[resultArray addObject:year];
NSLog(@"result array is %@",resultArray);
}
else{
NSLog(@"Not found");
}
sqlite3_reset(statement);
}
Its always going to else part and displays "Not Found". Am I doing something wrong? Is my join query is right? I am new to join concept so please help me , your ideas are totally welcome.
Upvotes: 3
Views: 530
Reputation: 1269803
This is your query, which I understand until the end of the on
clause:
Select EMPLOYEE.*
from EMPLOYEE INNER JOIN
DEPARTMENT
ON EMPLOYEE.empID = DEPARTMENT.empID =%@",_txtfind.text];
Perhaps you want where
clause of some sort?
Select e.*, d.col1, d.col2, . . .
from EMPLOYEE e INNER JOIN
DEPARTMENT d
ON e.empID = d.empID
where e.empId = 'txtfind.text'; -- something like this
In any case, if this doesn't help, then edit your question and show the SQL query after variable substitution.
Upvotes: 2