-1

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

Shirish11
  • 1,517
  • 2
  • 16
  • 37

2 Answers2

2
  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.
da-soft
  • 7,430
  • 25
  • 34
  • having a single Table is not an option for me cause it will be too lengthy and both are having different functionality.And I m not providing an Edit option for the results(its just for the display purpose) – Shirish11 Oct 17 '11 at 10:23
  • Ok, then (1) and (3) are actual for you. Although "too lengthy" and "different functionality" are not good arguments without details. – da-soft Oct 17 '11 at 10:35
  • I have 2 DBGrid on other forms that display each of the Tables. and combining both the tables will produce 30 odd columns which is hard for me to handle – Shirish11 Oct 17 '11 at 10:40
  • 1
    You have few options to show only required columns - persistent fields, TField.Visible and persistent columns of TDBGrid. – da-soft Oct 17 '11 at 11:11
  • ok I got it instead of assigning the `table datasource ` i have to assign the `query datasource` and there u get it. – Shirish11 Oct 18 '11 at 04:11
-1

The DBgrid is probably not the component you need. Give an eye to the TTreeView

http://delphi.about.com/od/vclusing/l/aa060603a.htm

Louis
  • 2,790
  • 1
  • 17
  • 23