Search code examples
mysqldelphiinner-joindbgrid

delphi DBGrid display JOIN results


I am working with BDS 2006,MySQL DB (MyDAC components used for connection) and i have a DBGrid component on my form which displays the records from my DB table.

Now i need to JOIN two tables and display the results in my DBGrid

The Resulting view that I should get is the result of the query

SELECT e_salary.e_basic,e_details.e_name FROM e_details INNER JOIN e_salary ON e_details.e_id=e_salary.e_id;

there is one more option to do it as I searched

SELECT e_salary.e_basic,e_details.e_name FROM e_details, e_salary WHERE e_details.e_id=e_salary.e_id;

e_details,e_salary are my two tables and e_id is my PRIMARY KEY

Presently I am having 2 DBGrid one is for e_details and other for e_salary

Is it possible to have only 1 DBGrid displaying values from both the Tables? or I have to display 2 separate DBGrid?

If possible then how do I go about it

P.S.- there are more columns to be added in the view and both tables have same no of rows

Thanks in advance


Solution

    1. DBGrid displays a dataset data. The data may be result of some SQL query execution. DBGrid, TDataSet and TDataSource do not cary what was the SQL query. Single table SELECT, multi table SELECT with joins, stored procedure call or SHOW command. So, yes - you can use 1 DBGrid to display resultset of your SELECT joining 2 tables.
    2. If both tables have the same number of rows, e_id is primary key for both tables, then why not to have single table, containing columns of both tables ? Also, if you will need to edit your dataset data, then there may be problems to update columns of both tables. And that may be one more argument to have single table.
    3. Although you can use WHERE e_details.e_id=e_salary.e_id instead of JOIN e_salary ON e_details.e_id=e_salary.e_id. The JOIN is preferred, because DBMS gets your intent more explicitly and that is more readable for others.