SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.table1_id WHERE table1.id = 1
I need to join only one column from table 2, say first_name. How can I do that?
Assuming that you mean "select one column from table 2": SELECT table1. *, table2. first_name FROM table1 LEFT JOIN table2 ...
SELECT * FROM table1 JOIN table2 ON table1. column_name = table2. column_name; The INNER JOIN in SQL joins two tables according to the matching of a certain criteria using a comparison operator.
To merge table columns in HTML use the colspan attribute in <td> tag. With this, merge cells with each other. For example, if your table is having 4 rows and 4 columns, then with colspan attribute, you can easily merge 2 or even 3 of the table cells.
The corresponding columns can have different names, as they do in our example. By default, the name of the corresponding column in the output will be taken from the first SELECT statement. If necessary, you can set the names for the resulting columns using the AS keyword.
Assuming that you mean "select one column from table 2":
SELECT table1.*, table2.first_name FROM table1 LEFT JOIN table2 ...
The accepted answer is the correct answer but I have encountered a strange error when the tables are in two different databases:
Assuming that table1 is in database1 and table2 is in database2. Initially I have tried this:
SELECT *, database2.table2.first_name FROM table1 LEFT JOIN database2.table2 ON database1.table1.id = database2.table2.table1_id WHERE table1.id = 1
The strange thing is that if I try this query from PHP PDO there were no errors but the result contained all columns from database2.table2 (expected only first_name column).
But if I have tried the same query from phpmyadmin got a sintax error:
Table 'database2.table1' doesn't exist
So, for solve that, then all databases need to be specified implicitly like this:
SELECT database1.table1.*, database2.table2.first_name FROM database1.table1 LEFT JOIN database2.table2 ON database1.table1.id = database2.table2.table1_id WHERE database1.table1.id = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With