how to join two tables from different databases? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Database 1 : sandbox
table 1 : coordinates
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
database 2 : phone
table 2 : workstations
I only have READ privilege
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
PROBLEM:
I only have READ privilege on that second table of that database.
I want to join both tables so I can display the "ws" field on screen for my PHP script.
My "station_number" field values are written differently from the "pod" field
(they have a zero in front of the letter if it is a single digit after the letter) does it make a difference?
I've seen examples online on how to join but for some reason I can't get it to work.
Do I need to create an extra field on my main Table to store the field values from "ws" or what? I'm a bit confused.
Thanks in advance!

Yes, it makes a difference if there's a 0 in one and not in the other. You'll have to modify the value of one or the other. Something like SUBSTR(station_number, 0, 1) + LPAD(SUBSTR(station_number, 2), 2, '0').
And to join the tables from different databases, you just have to put the database name in front of the table name. Probably something like sandbox.coordinates.
So your query might look something like this:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON (SUBSTR(c.station_number, 0, 1) + LPAD(SUBSTR(c.station_number, 2), 2, '0')) = w.pod
If you can update one of your tables to make the columns match, then your query is as simple as:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON c.station_number = w.pod

Related

Get data from more then 2 table against 1 id in php sql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have four different table. And invoice_id is foreign key in other table.
i want to show all records against invoice_id.
invoice_id | Bill_amount | Commission_amount | Payment_amount |
| | | |
2 | ------ | 1000 | 500 |
2 | ------ | 200 | 100 |
2 | ------ | -------------- | 100 |
2 | ------ | ------------ | 50 |
3 | 100 | 200 | -------- |
And So On........
Suppose you need 4 columns from 4 different tables. You can use left join if you want to include the empty records also from table2, table3 and table4.
Try the following query:
SELECT
table1.invoice_id,
table2.Bill_amount,
table3.Commission_amount,
table4.Payment_amount
FROM
table1
LEFT JOIN table2 ON table1.invoice_id = table2.invoice_id
LEFT JOIN table3 ON table1.invoice_id = table3.invoice_id
LEFT JOIN table4 ON table1.invoice_id = table4.invoice_id
WHERE
table1.invoice_id = '(your_required_id)';

Which is the better way to create my mysql table? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Based on my situation, should I combine all symptoms into one row as my method 1 below or method 2 that create more rows. The reason I don't like method 1 is that I have to use - to separate each symptom, and later I need to use php explode('-') to separate them and use LIKE to match them.
Method 2 will create more rows, and I think I will create more table to separate them.
Method 1:
disease symptoms
HIV pain-cough-hair loss
Flu cought-running nose-fever
cacer lose weight-fever-fatigue
Method 2:
disease symptoms
HIV pain
HIV cough
HIV hair loss
... ...
... ...
Out of your two methods, method 2 would be preferred. As #JNevill notes, storing multiple pieces of data in one column becomes a nightmare when searching or filtering data.
My full recommendation would be to use option 3 however. Take a look at the below design:
Table 1: DISEASES
+------+-----------+
| id | name |
+------+-----------+
| 1 | HIV |
|------|-----------|
| 2 | FLU |
|------|-----------|
| 3 | Cancer |
+------+-----------+
Primary Key:
id
Table 2: SYMPTOMS
+------+-----------+
| id | name |
+------+-----------+
| 1 | pain |
|------|-----------|
| 2 | cough |
|------|-----------|
| 3 | hair-loss |
+------+-----------+
Primary Key:
id
Table 3: DISEASES-SYMPTOMS
+-------------+--------------+
| disease_id | symptom_id |
+--------------+--------------+
| 1 | 1 |
|--------------|--------------|
| 1 | 2 |
|--------------|--------------|
| 1 | 3 |
+--------------+--------------+
Primary Key:
(disease_id, symptom_id)
Foreign Keys:
DISEASES.id -> DISEASES_SYMTPOMS.disease_id
SYMTPOMS.id -> DISEASES_SYMTPOMS.symptom_id
Establish your base tables DISEASES and SYMPTOMS. Then establish a 3rd table representing a JOIN of the first two tables. This normalization of the data will simply the structure of your application and prevent duplication of data since each disease can have multiple symptoms and each symptom can belong to multiple disease.
SAMPLE QUERY (MySQL):
SELECT
d.id,
d.name,
s.name
FROM DISEASES as d
INNER JOIN DISEASES_SYMPTOMS AS ds ON d.id = ds.disease_id
INNER JOIN SYMPTOMS AS s ON ds.symptom_id = s.id;
SAMPLE QUERY RESULT:
+------+----------------+----------------+
| id | disease_name | symptom_name |
+------+----------------+----------------+
| 1 | HIV | pain |
|------|----------------|----------------|
| 1 | HIV | cough |
|------|----------------|----------------|
| 1 | HIV | hair-loss |
+------+----------------+----------------+
It depends how normalized you want your database to be. The more normalized approach would be to create a symptoms table that houses all symptoms, a disease table that houses all diseases, and another table with links diseases to symptoms probably by disease_id and symptom_id. And the less normalized approach is like your method 1 where you include all the symptoms as a field in the table separated by some delimiter or put into an array if you are using a database that supports arrays.
Create two master tables for disease & symptoms. And then create a third table say .disease_symptom include the two foreign key column say disease_id & symptom_id and refer to the corresponding master table

Mysql - select from multiple tables without producing duplicate data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have three tables and I would like to select from the tables without producing duplicates.
The table are as follows:
Customers
id | name | lastName
---------------------------------------
1 | john | doe
2 | helen | keller
Orders
The userID column is a foreign key that references John Doe, so John orders 3 items.
id | userID | order
---------------------------------------
1 | 1 | pizza
2 | 1 | pasta
3 | 1 | lasagna
CustomerRating
The userID column is a foreign key that references John Doe, so John leaves 5 reviews.
id | userID | rating | comment
-------------------------------------------------
1 | 1 | 5/5 | was good
2 | 1 | 5/5 | excellent
3 | 1 | 4/5 | great
4 | 1 | 4/5 | great
5 | 1 | 4/5 | great
How would I select from the 3 tables where I can get a return results that look like this?
id | name | lastName | order | rating
-----------------------------------------------------------------
1 | john | doe | pasta | 5/5
| | | pizza | 5/5
| | | lasagna | 4/5
| | | | 4/5
| | | | 4/5
I've tried joining these tables, but since John has left 5 reviews and only ordered 3 times, the id, name,lastName, and order columns gets filled with duplicate data.
Thanks!
I don't have any experience in MySQL but I assume that it works similar to MSSQL.
So the format in which you are expecting the output is not possible. You can rather get the order and rating column values as comma separated
Here is a similar kind of question that might help you
including example based on link
try something like this
SELECT Customers.id, Customers.name, Customers.lastName,
GROUP_CONCAT(Orders.order) OrderedDishes,
GROUP_CONCAT(CustomerRating.rating) RatingsGiven
FROM
..... rest of your query .....
There are ways to discard duplicates (SELECT DISTINCT, UNION, GROUP BY) but it is not clear whether users update existing rating or create new ones. And what you want to see: the last rating or the average one
On the other note - i would change your entire setup:
order table would contain order_id, customer_idand other order related stuff like order_date
products table that would describe each of your dishes and their info like price, description etc
order_products table with fields order_id and prduct_id
if users rate products then your rating table would need at least product_id, customer_id, rate_value. I'd also add ratingDate That way you can get averages or select the last one by Max(ratingDate)
I think you need to add an orderID field to the CustomerRating table else there is no way to relate an item to its rating.

Compare three tables for one answers in MySQL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been working with MySQL for a while now, and just recently found the need to manage my data better (MOAR DATA!)...
The problem I am having is this:
Table1: users
- id
Table2: companies
- companyid
- companyname
Table3: customers
- customerid
- companyid
I am trying to query the following.
I have the users ID, I need to use that to get the companyid from customers using the customerid, and return companyname based of the assigned companyid in customers.
It is very possible I am going about this very wrong. I understand that eventually the data is going to get very hard to read by eye as the data starts to grow. My concern is having the ability to associate and disassociate customers from businesses.
If you have any tips, or have a better strategy, or think I should just add this information into the users tables please let me know.
First off, you need some understandings on what Normalization is: http://support.microsoft.com/kb/283878
The database tables are not meant to be "read by eye". I'm pretty sure you are dealing with a very small database now, but imagine in the future you're dealing with thousands of tables with millions of rows, "visual inspection" is not going to work anymore.
A simple join would have given what you need:
SELECT t2.companyname
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t3.customerid
AND t3.companyid = t2.company id
AND t3.customerid = (some id) //Depends on what your purpose is,
//this line can also be replaced by
//AND t1.id = (some id)
In your case, it is possible to combine User table and Customer table into one ONLY if all users are customers too. But it is definitely a NO to have company information in either User or Customer tables.
Assuming customers.customerid and users.id will be the same value, this should suffice:
SELECT companies.companyname
FROM customers
LEFT JOIN companies ON customers.companyid = companies.companyid
WHERE customers.customerid = 5
Here is a fiddle
Schema is on the left, sql is on the right.
Tables:
users
+--------+
| ID |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
+--------+
companies
+------------------------------------------+
| COMPANYID COMPANYNAME |
+------------------------------------------+
| 5 CompAlumpany |
| 9 Dergy Hergins LLC |
| 3 Smergy Berg Inc. |
| 23 Hergin Derz |
| 7 Comperation corpany |
| 11 Contagion Engine |
| 31 AEther Vial |
| 66 Necropotence |
| 90 Lord of Atlantis |
| 65 Snoogins |
| 51 Nickty-Schnickty-Schnoine |
| 58 Take a knee |
| 59 Coorprate |
+------------------------------------------+
customers
+--------------------------+
| CUSTOMERID COMPANYID |
+--------------------------+
| 1 5 |
| 2 9 |
| 3 3 |
| 4 23 |
| 5 7 |
| 6 11 |
| 7 31 |
| 8 66 |
| 9 90 |
| 10 65 |
| 11 51 |
| 12 58 |
| 13 59 |
+--------------------------+
Query Returns:
+---------------------+
| COMPANYNAME |
+---------------------+
| Comperation corpany |
+---------------------+
Posted on behalf of the OP.
I got the results I was looking for with the following:
SELECT t2.companyname FROM companies t2,customers t3 WHERE t3.companyid = t2.companyid AND t3.customerid=?
I was unaware I could create references, this will become very useful for me. Thank you!

How do I sort data from a mysql db according to a unique and predetermined order, NOT asc or desc

I need to show 1000 test questions to a student, 10 per page.
The questions are in a mysql table, the answers will go in another table.
I need each students questions to appear in a different predetermined order than any other students. The sort order is predetermined when they register and placed in a usermeta table.
In the usermeta table there is a column that lists the order in which the questions should be shown. The order in that column is unique to each student and looks like this example: 8|14|97|54|21|37|54|61 ...etc.
The first question to be shown to the student would be question #8, and then question #14, and then question #97, and so on, listing 10 per page.
I don't need to sort the questions asc or desc. Also, I can change the db structure if that would help find a solution.
Also, I can change the db structure if that would help find a
solution.
If changing the db structure is possible, then instead of storing the sorting order as a pipe separated string, store it in a separate table that maps each question to the order it should appear in for a given student. i.e.
student_id, sort_order, question_id
1 1 8
1 2 2
1 3 97
Then join on your sorting table when selecting your questions for a particular student.
SELECT q.* FROM
questions q
JOIN questions_sorting_order qso
ON q.id = qso.question_id
ORDER BY qso.sort_order
WHERE qso.student_id = :student_id
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT i2.i*10+i1.i x
, FIND_IN_SET(i2.i*10+i1.i,'8,14,97,54,21,37,54,61') y
FROM ints i1
, ints i2
HAVING y > 0
ORDER
BY y;
+----+---+
| x | y |
+----+---+
| 8 | 1 |
| 14 | 2 |
| 97 | 3 |
| 54 | 4 |
| 21 | 5 |
| 37 | 6 |
| 61 | 8 |
+----+---+
Note that 54 is ignored second time around

Categories