Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7983465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:57:54+00:00 2026-06-04T10:57:54+00:00

USING MySQL. I have two tables. Table A, and Table B. Table A has

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:57:55+00:00Added an answer on June 4, 2026 at 10:57 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using php and mysql I have two tables, a users table and a profiles
Need some help with putting this query together. I'm using Mysql I have two
I have two MySQL tables: product has id , title , price and some
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
I am using MYSQL and PHP. I have a table called item. Two of
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I have a MySQL table from a third-party application that has millions of rows
I have a database using MySQL 2005. I have two tables, Enrolment and AlertMsg.
I have two tables that get joined regularly. Table One is about 1 Million
This question requires some hypothetical background. Let's consider an employee table that has columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.