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.
Following
UNIONwould get you the output for your given inputsbut there are several problems with your table design (as already mentioned by JohnFx)
'null'string value in any database.parent_idis linked tocrm_field. I would suggest to renameTableB.parent_idtoTableB.crm_fieldmaking that link clear.TableB.foorecord 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 to0as foreign key.