Inner joining 2 columns from different tables in MYSQL workbench - php

Sorry, but I'm quite newbie in SQL and PHP. Previously, I was able to easily join 2 tables columns in PHPmyadmin with the design button with out any PHP. However, I'm trying to retrieve my database with python, so I have to MYSQL workbench.
I'm trying join 2 columns on seperate tables, but I can't get it to work. Firstly, I've watched several tutorials and I really can't understand PHP and all the scripting. Secondly, I tried joining the 2 tables when in reverse engineer since it seems quite similar to PHPmyadmin, however I cannot select individual columns while in reverse engineer, even if I try to index the columns.
IMAGE
I'm trying to join or make class1, class2, class3 from student table equal to classid from the class table. Is it possible at all to join the 2 tables here so it would actually show a linking between 2 specific columns? I can easily add a line but it always selects the primary key.
I appreciate any help, thanks. Alternatively, if I could get help in developing a PHP script, which also shows the linking between 2 tables, that'd also be great.

Related

Many-to-Many Relationship Query

Right now I'm trying to query a table that holds the id of two tables that holds the id to two other tables (where one of these holds the id to other tables)
Hell, I know.
As I am a beginner in SQL, how do I go about efficiently querying this to get all the details of each table? As of now i only see a very long set of Select statements. Here is how the tables are set up below
I am not sure what you are asking.
If you need a SELECT command it would have 2 INNER joins. But, if you are asking to get all the info (and children) then it would need more INNER joins. OR if you are using a persistent client for your application.... I just don't know. Because it is incomplete data to do it.

Copying data in MySQL with cross-referenced rows

I'm not a database expert, so I'm not sure how to ask this question briefly and succinctly. I am trying to copy data with the following characteristics: many of the tables with data being copied contain references to other tables with data being copied; i.e., a patient might attend a class where their weight is recorded, so I need to copy both the class attendance row as well as the weight value stored in another table, which is referenced by the class attendance row. There are other, even more complex, examples in this database, but it seems that I need to perform some kind of recursive copy of these inter-referenced items so I can maintain the cross-references in the copied data.
So, is there any kind of standard approach to this problem? If there isn't a direct answer, could someone share the terminology of what I'm trying to do so that I can look it up on my own? I'm certain this problem has been tackled many times before, but I don't know how to find the solution. I understand the basic concepts of JOINs and FKs, but this solution seems to require a way to copy the rows from various tables while also going back and updating the cross-references (in some cases, these are FKs, and in other cases, they are not; I'm stuck with the schema as it is).
PS: If it's such an obvious solution, why won't anyone just provide it or characterize it below so we can move on? Most of humanity is capable of asking the occasional dumb question, and this may very well be one of mine, but I'm seriously stuck on this one and would appreciate some assistance.
Here's a sketch of a small part of the schema to try to illustrate the issue:
When we copy a patient's data record, we need to 1) create a new row in patient; 2) create a corresponding new row in edclass_session_labs; 3) create a new row in patient_lab_weight; and (here's what I see as the tricky part) 4) also update the reference in edclass_session_labs to the new row in patient_lab_weight. What I'm looking for is a way to do this programmatically and algorithmically. I'm sure problems like this have been tackled before, so that's why I'm asking for advice here.
I didn't fully understand what you mean by "copy patient data", so there are two options:
1) If you want to "copy" the data to a report, you need to link many tables with related information, so you have to study the concept of JOINs and FOREIGN KEYs. This is what we do when we need to convert relational data into a flat table that can be easily read by non-IT people.
2) If you need to copy specific data from database tables to other database tables, you also have to study FOREIGN KEYs and table relationship. You need to understand how table rows relate to rows on other tables (one to many, many to one, many to many), so you can create INSERT statements based on SELECTs that will filter the exact data you need.
This is very general, but I think it's sufficient to point you to the right direction.
EDIT:
Since the issue is related to creating a merged structure of patient data, let's say we have patient 1 and patient 2. They are duplicates of the same person, and need to be merged. I would do this, in this order:
a) Create a patient 3, this one will be the target of our merging. Simply copy each field from patients 1 or 2 to this new record.
b) Create as many new records as needed in table "patient_lab_weight". For example: if patient 1 has 2 records there, and patient 2 has 4 records, you will have to create 6 records, which are copies of the records related to patient 1 and 2, but patient_id will be 3. However, after creating each record here, obtain the auto_increment generated for field "patient_lab_weight_id", and insert a new record in "ed_class_session_labs", with patient_id = 3, and "patient_lab_weight_id" = the obtained ID. Do that for each insert on "patient_lab_weight".
c) after all that, disable patients 1 and 2 in your application.
If you use this approach, you will slowly build up your new structure, linked in a consistent way.

CakePHP using a table to show relation between two tables

So I was building a cake project with a coworker the other day and he used a table in the database (users_credentials) to create a link between two constant tables (users and credentials.) When I tried to duplicate that result with a project I'm working on alone, it isn't working. Can anyone throw me a bone? I just need something to Google or a name or something.
Thanks
Justin
I think you want to connect a user to some credentials? What you can do is give each row an unique number (primary key) and combine those two keys in another table.
You could search google for "foreign keys and primary keys" I think, or search for an ERD (Entity-relationship diagram).
You will likely need to just do a join of the 3 tables. The CakePHP user guide offers some help on how to do that assuming you've got your application hooked up to the database correctly. It looks like Cake also does some of the heavy lifting for associating your Models with one another as well as documented here. I'm more familiar with CodeIgniter's syntax, but the basic idea is the same:
select something from users
join users_credentials on user id
join credentials on credential id

When using cakephp habtm, do I have to create relations in MySql too?

Or is it enough to have just the relations defined in the model. I have been trying to connect tables users and groups and got only to a point where only last connection in array got saved ...
Just to add, I am using table users_groups as a join table. This table tas fields user_id and group_id. Find all works like a treat.
All you need to do as far as your database is concerned is to make sure you're using the correct table name and field(s). Then, as long as you have your model associations set up correctly, you should be good to go.
In your case, your table should be 'groups_users', not 'users_groups' (they should be in alphabetical order).

Is it a bad idea to connect two databases like this? How can it be done?

I am currently using Simple Machines Forum on my website, and users both register AND login using their forum account on my website. The smf_members table contains fields such as:
id_member | member_name | date_registered
What I am trying to do now is extend this to add more custom fields and connectivity on my site. I want to use the id_member field for many things:
For example, I want a user (an entry in this smf_members table) to be able to join a team.
I am going to create a table Teams with the following fields:
ID | Name | Description
and a table TeamMembership with the following fields:
UserID | TeamID | Role
As you can see, this table will link a member and a team, while leaving data specific to a user or team in their respective tables ONLY (successfully preventing redundant data). This sounds good, right?
Well, I don't want those two new tables in the same database as the SMF stuff, because it may get messy. Is it the easiest solution, though? Do you think the easiest solution is to just create my new tables within the same database, with the prefix cst for Custom? I have no idea how to link two databases so if it is too complicated maybe I should just do my cst solution.
I've edited this post and have an additional question.
Thank you for the answers. I have an additional question. Let's say that I wanted to extend new variables to the members, but again, wanted to avoid adding new fields into the SMF forum member table. What is the easiest way about going about this? Like, I want to create a table called UsersExtended and have fields such as:
-ID (this is NOT an auto-increment field, but the value of id_member from the SMF members table)
-Country
Is it easy to create a profile page with this structure and display any relevant data I want from the two tables, in a way, Linking the two so that they act as one big table?
They totally belong in the same database. Use one db and join the tables in your queries.
The only time you want to build a unique database for your application (other than for the application itself) is if you intend to create an API which will serve up that data to other projects in an unbiased manner. That is very common with Solr in which you can churn out blazing fast API's and generally don't belong mixed in with your current MySQL tables.
There is absolutely no harm in having the custom tables in the same database. You cannot define relations between 2 different databases in a relational database system.
If you're using MySQL have a look at this question's accepted answer. Remember you have to prefix your table with database name when querying across databases.
You may try this
SELECT *
from database1.table, database2.table
where database1.table.id=database2.table.id
You should only create two tables within the same database
**smf_members**
id_member | member_name | date_registered
**TeamMembership**
Team_ID | Name | Description | Role | id_member
If any user wants team membership the you can easily manipulate its record using join queries.
Don't know Simple Machines Forum - but I do know that most applications expect to be the only show in town.
I would create a new database = perhaps called "teams" with your own tables, and use NickCL's way of doing cross-database joins to join between the two applications.
You don't mention the specific database engine you're using (I assume it's MySQL), but logically, you can think of databases as namespaces - a way of keeping stuff that belongs together in the same logical place.
Ideally, "people"/"authentication" etc. should be in a separate database from the forums stuff - but as you're working with off-the-shelf code, you don't have that luxury.
In my experience, it's better not to mess with the databases of off-the-shelf software - when you want to upgrade, you have no idea what will happen to your own tables, regardless of their names....

Categories