Connect Tables in MySQL - mysql

I'm attempting to connect two tables in MySQL so that when selected, a value is pulled from the table.
Table 1
id
mobile_car
Table 2
id
mobile_car
car_domain
When the mobile_car is selected via a dropdown and the user registers this, I would like to be able to have it connect to the car_domain so I can call this later in the application.
For example, if the user selects "Verizon" as their mobile_car, I would like it to link to the car_domain "#vtext.com" so I can connect their phone number with the car_domain. Essentially, I'm sending a text by way of emailing their cell number.
I know there are programs and pre-built options, but I'd like an understanding of how this could be done.
Thanks!

Try joining your two tables together:
SELECT t1.mobile_car, t2.car_domain
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.mobile_car = t2.mobile_car
I'm also assuming that your actual table structure will be more complex than this, since right now you can actually just query Table 2 by itself to get what you need.

Related

How to query my tables to give data in such a format?

I have developed a structure to represent hierarchical data in in my db. Now I have 3 tables with relationship defined with each one.
How should I query my db to get the data in such a structure.
Query I have to get data from separate tables
(Gives me all the users with product in the table)
List discUsersConfs = newArrayList(ao.find(DiscUserConf.class, Query.select()));
(Gives me all the effects with users in the table)
List discEffectConfs = newArrayList(ao.find(DiscEffectConf.class, Query.select()));
How can I combine these queries to get the data in way I have shown the relationship in the below image.
I am using Active Objects which is one of the ORM.
I normally would do it like this.
select all from table1
while fetching{
select again but from table 2 where user = (user from the table1)
...and so on..
}
This is a problem and it has more than one solution. The problem with my method is that for each deeper level you must give more table and add them manually to the code

How to "inner join" two website DB's

I have a requirement and would like to know the best way of achieving the goal.
The idea is to "inner join" two tables from two databases from two completely different websites.
For example, I have a website that tables ID's from another source. I then want to analyse those ID's in more detail by joining my table with the other websites table.
My initial thought would be to pass a JSON list of ID's from my table to a php file on the 3rd party host, which would then do a "select fields from table where id in (JSONList)", then pass back the information of which I would then stitch together and display.
Is there a better / easier way of doing this?
You can do it directly in the database, which allows you to use sql.
MySql will allow you to create a Federated Table locally, that when accessed will pull the data automatically from the remote database.
Once you've got your federated table setup you should be able to write your join normally e.g:
select * from table1 inner join table2 on table1.joinkey = table2.joinkey;
There are a number of related questions already if this is the approach you wish to take (or at least try):
MySQL: SELECT from another server
I need to join table from other database and sometimes other server
https://dba.stackexchange.com/questions/81411/select-in-federated-table-is-to-slow
Unfortunately the docs say this will only currently work for mysql to mysql: http://dev.mysql.com/doc/refman/5.1/en/federated-create-connection.html

Make tables related

I'm trying to design a database for my website but i have a little problem with making related tables .
I have two tables . Table1 and Table2
Table1 is used to store user`s information and Table2 is used to store their activation codes
To relate them i need to store each user's ID in Table1 , inside Each record of Table2 so that i can get my user`s activation code
And as i know it is how relational databases work.
My problem is when using php to run mysql commands and add new user information in Table1 , How can i find What ID Number will be assigned to my user so that i can save both Id and activation code in my other table ?
If you use PDO, you can use the function lastInsertId() to retrieve the last id inserted in your last query.
http://php.net/manual/en/pdo.lastinsertid.php
If you use Mysqli, you have the mysqli_insert_id() function .
http://php.net/manual/en/mysqli.insert-id.php

SQL Inner joining a column from two tables with a single user id

I've been trying to get this SQL query running for a while now and can't seem to get the last little bit going.
The backend database to all this data is a Drupal install with data spread out across a number of modules, so I need to do a lot of joining to get a certain view table set up that I need for a third-party application.
It's hard to explain the entire schema, but here's the sqlfiddle:
http://sqlfiddle.com/#!2/68df0/2/0
So basically, I have a userid which I map to a profile id through a join. Then I need to use that profile ID to pull the related data about that profile from two other tables. (there should only be one row with each pid in each of the other tables)
So in the end, I would like to see a table with username, othername, and key_id.
I got the first two pieces in there, but just can't seem to figure out how to join in the othername, and keep getting null.
The server is running MySQL.
Thanks guys!
LEFT JOIN other_name
ON profile_link.pid=other_name.pid;

How to get a table from two databases?

Using Access 2003
I want to get a table value from the two databases
Database – 1
Emp_Table
Database – 2
Customer_Table
Select * from Database-1.Emp_Table, Database-2.Customer_Table
The above query is showing error in the Access. I tried a Join query also, it showing error.
Can any one to solve this problem?
Need Query Help.
Try using square brackets -
SELECT * FROM [Database-1].[Emp_Table], [Database-2].[Customer_Table]
Or, try this.
First, assuming the query is running in Database 1, you will need to create a "linked table" to link to Database 2's table in Database 1.
Once you do that, you can write it simply as:
Select * from Emp_Table, Customer_Table
Since you are "in" database 1, you won't have to qualify Emp_Table, and since you have Database 2's Customer_Table linked in, you won't have to qualify it either.
I don't have Access 2003, but in Access 2007 you can do this:
Click on the "External Data" tab.
Click on the "Access" icon.
Choose the location of your second Access database.
Select "Link to a data source by creating a linked table".
This should add the tables in your second database linked in your original one. You can then write queries to query data from either one or both like you normally would. I'm sure the same functionality is available in Access 2003, just a slighty different visual route to achieve the same thing.
Is there some relationship between the tables, or do you want just a dump of the whole table? Also, post the join you tried and the error you got, it would help in the troubleshooting...
If you want all records from both tables, you would need to use a UNION query like this:
Select * from Database-1.Emp_Table;
UNION Select * from Database-2.Customer_Table;
This assumes that there are the same number of columns in both tables. If not change the * to the specific columns you want to list from each table.