magicianiam
magicianiam

Reputation: 1579

Sql query to display data from table A and data from table B where parent_id of table A is found in table B using join

USING MySQL.

I have two tables. Table A, and Table B.

Table A has some values that has an ID related to Table B. this id is identified as crm_field in table A and parent_id in table B.

i tried doing this query.

select * 
from tableA inner join tableB ON tableA.crm_field=tableB.parent_field 
WHERE tableA.id = '75' AND 
      tableA.category != 'null' AND 
      tableA.category != 'No Category'

my expected result is that the data from tableA that met the where requirements will display along with that of tableB that holds the parent_id equivalent to that of the crm_field in tableA.

actual result is tableB being displayed with the exception of the parent_id that holds a value. also it seems that it queried when crm_field = 0 and parent_id = 0, when i want it to query when crm_field has an actual value like 22 that is also found in parent_id. tried doing this instead:

select * 
from tableA inner join tableB ON tableA.crm_field=tableB.parent_field 
WHERE tableA.id = '75' AND 
      tableA.category != 'null' AND 
      tableA.category != 'No Category' AND 
      tableA.crm_field != '0'

but it only showed the data from tableB that i needed to add to tableA.

is there any way to do this using a join query?

if not i might as well do a double query.

thanks

Sample output:

tableA  
id|name|crm_field|category  
0|dog|0|hi  
1|cat|22|hi  
2|bear|0|null|  


tableB  
id|name|parent_id|  
0|wild|22|  
1|foo|0|  

display should be something like this:

0|dog|0|hi  
1|cat|22|hi  
2|wild|22  

-if this is even possible?

to get to the point what i'm trying to accomplish here is this:

crm_field has some values.

but if the query sees 22 or 21 or both in crm_field it should then display the corresponding value that is found in tableB.

for know i did this if crm_field == 21 or 22 it will do another query to add the corresponding values to an array. but i want to accomplish this with less code as possible thus i was experimenting with join.

Upvotes: 0

Views: 1983

Answers (1)

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58431

Following UNION would get you the output for your given inputs

SELECT  id, name, crmfield
FROM    TableA
WHERE   category != 'null'
UNION ALL
SELECT  a.id, b.name, a.crmfield
FROM    TableA AS a
        INNER JOIN TableB AS b ON b.parent_id = a.crm_field
WHERE   parent_id != '0'        

but there are several problems with your table design (as already mentioned by JohnFx)

  • You should never store a 'null' string value in any database.
  • It is much easier (to me at least) to have your foreign keys named as the primary keys they link to. There's no way to know that parent_id is linked to crm_field. I would suggest to rename TableB.parent_id to TableB.crm_field making that link clear.
  • You don't want the TableB.foo record returned in your result. That strikes me as odd. I can imagine that you only want some parts of a relationship returned but here you seem to have attached some special meaning to 0 as foreign key.

Upvotes: 1

Related Questions