[英]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. 使用MySQL。
I have two tables. 我有两张桌子。 Table A, and Table B. 表A和表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. 表A具有一些与表B相关的ID的值。此ID在表A中标识为crm_field,在表B中标识为parent_id。
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. 我的预期结果是,将显示满足where要求的tableA中的数据以及具有与tableA中crm_field等效的parent_id的tableB数据。
actual result is tableB being displayed with the exception of the parent_id that holds a value. 实际结果是显示tableB,但拥有值的parent_id除外。 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. 还似乎它在crm_field = 0和parent_id = 0时查询,当我希望它在crm_field具有实际值(如22)时也要查询时,也可以在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. 但是它只显示了我需要添加到tableA中的tableB中的数据。
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. crm_field具有一些值。
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. 但是,如果查询在crm_field中看到22或21或两者都显示,则应显示在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. 知道我这样做,如果crm_field == 21或22,它将执行另一个查询以将相应的值添加到数组。 but i want to accomplish this with less code as possible thus i was experimenting with join. 但是我想用尽可能少的代码来实现这一点,因此我正在尝试加入。
Following UNION
would get you the output for your given inputs 跟随UNION
将获得给定输入的输出
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) 但是您的表格设计存在一些问题(如JohnFx所提到的)
'null'
string value in any database. 您永远不应在任何数据库中存储'null'
字符串值。 parent_id
is linked to crm_field
. 无法知道parent_id
已链接到crm_field
。 I would suggest to rename TableB.parent_id
to TableB.crm_field
making that link clear. 我建议重新命名TableB.parent_id
到TableB.crm_field
制作链接清楚。 TableB.foo
record returned in your result. 您不希望在结果中返回TableB.foo
记录。 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. 我可以想象您只希望返回关系的某些部分,但是在这里您似乎已经将0
作为外键附加了一些特殊含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.