Jump to content

Selecting from 2 tables with one reference table


axl8910

Recommended Posts

Hi, here are my tables:

 

FLEXI

 

ID          |name|  designation  |  status  |  PLC  | 

id_flexi|flexi1|(not important) |                |              |

id_flexi|flexi2|      .....                |                |              |

id_flexi|flexi3|    .....                |                |              |

 

USER

 

id_user|username|............|phone

 

FLEXI_ALLOCATION

 

id_flexi|id_user

 

In table flexi I have 7 fields(7 names), table USER changes when user registers on page, and FLEXI_ALLOCATION table changes when user reserves one item. In table FLEXI_ALLOCATION I have id from user and id from item he reserved, so in select I need to add username and phone from table USER to name, status PLC from FLEXI where their id matches in table FLEXI_ALLOCATION and fields username and phone should be "none" when there is no match. I need all that in one table so I can print it with mysql_fetch_array in PHP. My table should look like this:

 

Flexi  |  Status  |  PLC  |  User  |  Phone  |

Flexi1|  ON      |  ON  |  john  |  253452|      John has id 2

Flexi2|  OFF    |  OFF| none  | none      |

Flexi3| ON      |  ON  | Ann    |  214331|    Ann has id 4

 

If my FLEXI_ALLOCTION is:

id_flexi  |  id_user

  1          |      2

    3          |      4

 

 

 

 

Link to comment
Share on other sites

Why do you "need" to? That's just forcing bad practice.

 

What do you get from the following?

SELECT username, phone,  name as FlexiName, status, PLC 

FROM flexi_allocation 
RIGHT JOIN flexi ON (flexi_allocation.flexiID = flexi.ID)
RIGHT JOIN user ON flexi_allocation.user_id = user.user_id)

WHERE flexi_allocation.flexi_id IS NOT NULL

ORDER BY flexi_allocation.flexi_id ASC

 

Obviously change field and table names as needed.

Link to comment
Share on other sites

I 'need' to because that are orders from superiors=) I already had database and PHP and it worked perfectly but yeah...=) This works like charm thx=)) but there is just one little thing if you can also help me with. I need to print ALL FlexiNames no matter if they have match or no. This just prints lines and flexis where users are allocated and I want to print them all even though others have empty cells

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.