INNER JOIN does not return database result

Asked

Viewed 1,120 times

1

I’m trying to use this code:

SELECT *
FROM tb_trabalhador
INNER JOIN tb_detalhe_trabalhador ON Tb_trabalhador.id = tb_detalhe_trabalhador.tb_trabalhador_id

And it returns the blank result.

Is there any way around this with another code?

tb_details_worker:

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tb_trabalhador_id` int(11) DEFAULT NULL,
  `tb_funcoes_id` int(11) DEFAULT NULL,
  `MedicaValidade` date DEFAULT NULL,
  `MedicaAnexo` longblob,
   (...)

tb_worker:

`id` int(11) NOT NULL AUTO_INCREMENT,
`Nome` varchar(200) DEFAULT NULL,
`Morada` text,
`Tipo` varchar(45) DEFAULT NULL,
(...)
  • 2

    Your query is in condition, if no result is returned it is because there is no ID to make the connection. Have you checked if there are any records related by that field?

  • Right in the bank this query returns some line?

  • my test is done in the Database before being placed in php code. The result is blank

  • Post the structure of the two tables involved, it is easier to help. Are you sure that the tb_worker detail table has a tb_worker name field ?

  • I have now changed the code with the table structure

  • Place a print of the result of your database. The only thing is that there may be no relationship between the tables.

  • Based on the comments in my reply, your problem seems to be another one relating to the insertion of data, not the collection of data. I suggest you open up a new question to deal with.

  • Two questions: 1. there are data on tb_detalhe_trabalhador? and 2. the foreign key is correctly configured?

Show 3 more comments

2 answers

4

Your Join syntax is apparently correct.

What remains you check, is whether when entering the details of the worker, if the value of the tb_trabalhador.id desired is being included in the tb_detalhe_trabalhador.tb_trabalhador_id, for the relationship to be complete, since the condition is precisely this:

tb_trabalhador.id = tb_detalhe_trabalhador.tb_trabalhador_id

To get the correct ID, if you are entering a new worker and their details in the same series of operations, one possibility is to use the function LAST_INSERT_ID( ) right after entering the worker, and then insert the details with the ID returned by the function.

Taking advantage, see this issue about joins, and note the Ids of the example tables and the results if the condition of the ONwhether or not.

2

Updating

After discussion in the comments of this reply, it is clear that your query is not returning results as by inserting the records into the database you are not applying to the field tb_trabalhador_id of your table tb_detalhe_trabalhador the value entered in the field id of your table tb_trabalhador.

With the exception of the typing error in the table name I mentioned in the "original answer", your query is well constructed and does not return results unless there are no results related by the fields indicated in it.


Original Response

Your query is in condition and well formatted, but I think you have a typo in the table name:

Tb_trabalhador

It should be with t minuscule:

tb_trabalhador

Or vice versa.

Consultation:

SELECT *
FROM tb_trabalhador
INNER JOIN tb_detalhe_trabalhador ON tb_trabalhador.id = tb_detalhe_trabalhador.tb_trabalhador_id

9.2.2 Identifier Case Sensitivity

In Mysql, Databases correspond to Directories Within the data directory. Each table Within a database Corresponds to at least one file Within the database directory (and possibly more, Depending on the Storage engine). Consequently, the case sensitivity of the underlying Operating system plays a part in the case sensitivity of database and table Names. This Means database and table Names are not case sensitive in Windows, and case sensitive in Most Varieties of Unix. One notable Exception is Mac OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive. However, Mac OS X also Supports UFS volumes, which are case sensitive just as on any Unix.

That translated:

In Mysql, the databases correspond to the directories within the data directory. Each table within a database corresponds to at least one file in the database directory (and possibly more, depending on the storage engine). Consequently, the sensitivity of the underlying operating system to upper or lower case letters plays a role in the sensitivity of upper or lower case letters in database and table names. This means that database and table names are not case-sensitive in Windows, but are in most varieties of Unix. A notable exception is Mac OS X, which is based on Unix, but uses a standard file system type (HFS+), which does not differentiate case from case. However, Mac OS X also supports UFS volumes, which are sensitive, just like on any Unix.

  • My mistake when putting the code here. Because where I tested it has all lowercase letters.

  • 1

    Only if there are no related records, otherwise everything is fine with your query.

  • I changed the question and left the structure of the tables. Just a question. When I enter the data in the DB in table tb_worker_id I say it is NULL?

  • 1

    Your field tb_trabalhador_id table tb_detalhe_trabalhador must contain the value of the field id of your table trabalhador. Otherwise your query will not return results because you are looking for a relationship that does not exist.

  • Of course not. If you are entering worker details means you already know which worker is, then you already have the ID (or should have it). If you are trying to create the worker record already with the details you can insert the worker, recover the id generated by the BD and insert the detail in the same query.

  • Your problem seems to be another one related to data entry, not data collection. I suggest you open up a new question to deal with.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.