MySQL - Workbench (PHP):
Tables:
TUsers (One to many relationship with TCompanies):
TUsers_CompanyID (FOREIGN KEY)
TUsers_UserName
TUsers_UserPassword
TUsers_ID (UNIQUE)
TCompanies:
TCompanies_CompanyName
TCompanies_CompanyContactNumber
TCompanies_CompanyAddress
TCompanies_ID (UNIQUE)
Is it possible to link multiple tables in a relational database without using the JOIN, or INNER JOIN query commands, without duplicating data in tables?
Thus speaking even another way of creating a relationship that makes the one table "point" to the other's data.
So that one can query the following and successfully retrieve all the data from both tables at once:
MySQL:SELECT * FROM TUsers;
See example above..
You can do it without "appearing" to use a join (ie, the word JOIN won't be in the query), but MySQL will still perform a JOIN...
SELECT *
FROM TUsers, TCompanies
WHERE TUsers_CompanyID=TCompanies_ID;
You won't be able to escape having to use a JOIN for how you want to display your data, but what you might want to do is create what's known as a VIEW, so that you don't actually have to type out the JOIN commands whenever you want to query for the user data:
CREATE VIEW UsersView AS
SELECT *
FROM TUsers a
INNER JOIN TCompanies b ON a.TUsers_CompanyID = b.TCompanies_ID
Then once the view is defined, you can just select from UsersView like so:
SELECT * FROM UsersView
...And it will return the users information as well as the joined company information. You can think of views as a way to simplify (or "compactify") more complex queries, because underneath the hood, it's actually the same thing as:
SELECT *
FROM
(
SELECT *
FROM TUsers a
INNER JOIN TCompanies b ON a.TUsers_CompanyID = b.TCompanies_ID
) UsersView
Related
First table:
Second table:
In the first Table, there will be multiple given_to with same taskid, and specific to that taskid i have set task in 2nd table.
Is it possible to obtain the task of a same taskid from multiple users to be printed in a table? If so How can we achieve it?
If possible, I also want to print the columns given_to of the task seperated by space.
Please Help
I'm not exactly sure I understood correctly what you want as a result, but as far as I get it you can achieve this with the correct SQL query, using a simple left join:
SELECT * FROM table1 LEFT JOIN table2 ON table1.taskid = table2.id
You might want to replace the SELECT * FROM ... part with the specific fields you are interested in.
For more information about joins (that is: merging results/columns from several tables into one query result) take a look at the MySQL reference manual on JOIN syntax.
I have three different tables I am trying to pull info from. Related in different ways. I am trying to find a Join statement that will allow me to pull the information a little better than using the two queries I am now.
Here is what I have
SELECT * FROM reports LEFT JOIN students USING (sID)
This allows me to properly merge the two tables. However - There is a row in the reports table named pID. I have another table named test, that also has a matching pID row, along with other data. I am trying to access the data from that table as well. Is it possible to join three tables in that way? Not only getting data from the sID, but the pID as well
You can do the following
SELECT *
FROM reports
LEFT JOIN students USING (sID)
LEFT JOIN table3 USING (pID)
Where table3 is the name of your table.
Hi robert yes it is posible. You can do a query like as shown below given your three tables, reports, students, and test.
SELECT *
FROM `reports`
LEFT JOIN `students` ON `students.sid=reports.sid`
LEFT JOIN `test` ON `reports.pid=tests.pid`
So, I am trying to select some data from 4 tables using a query I have attempted to throw together.
SELECT *
FROM cards
LEFT JOIN cards_viewers ON cards.card_id = cards_viewers.card_id
(SELECT *
FROM folders
WHERE folder_id = cards.card_folderID)
(SELECT user_firstName,
user_lastName,
user_avatar
FROM user_data
WHERE user_id = cards_viewers.user_id)
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
Basically, the query selects data from the four tables depending on the user_id in table user_data. I have attempted to initially fetch all data from the tables cards, and cards_viewers, and have went on to use this data to select values from the other tables (user_data and folders).
The query is wrong, I know that. I have learnt the majority of basic MySQL, but I am still struggling with more complex queries like the one I am trying to write now. What query can I use to select the data I want?
Links to any documentation to parts of queries would prove very useful in helping me learn how to create queries in future, rather than just relying on StackOverflow.
Many thanks.
You don't need "MULTI-WHERE" but multiple joins, you just need to keep doing joins until you get the tables you need.
Here's an example:
SELECT *
FROM cards LEFT JOIN cards_viewers
ON cards.card_id = cards_viewers.card_id
LEFT JOIN folders
ON folders.folder_id = cards.card_folderID
LEFT JOIN user_data
ON user_id = cards_viewers.user_id
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
To custom the fields you want to get just change * for the name of the field being careful about ambiguous column naming.
For further information check MySql Joins. Hope this helped you :)
I have two tables that I am trying to join with a column called "job_id". The first table, "Commitments", contains information about project details, the second table, called "Jobs" contains details about the project managers assigned to a job.
I want to join the two together and return all the rows from "Commitments" that correspond to their project manager (Commitments.Project_Manager).
So, a project manager is assigned multiple projects (contained in jobs), and each project has multiple details about it (in Commitments), so going up the chain, my query should return all the rows going up the chain in Commitments that are associated with a project manager in Jobs.
This is my current query, but it is simply returning all of the results from Commitments:
$sql = "SELECT * FROM Commitments
LEFT JOIN Jobs
ON Commitments.job_id=Jobs.job_id
AND Jobs.Project_Manager LIKE :projectmanager"
This is being done in PHP/MSSQL with an ODBC connection between the two.
Sorry if this is something really simple, I'm terrible with SQL queries. Thanks!
If you want Project_Manager even without commitments:
SELECT *
FROM Jobs LEFT JOIN Commitments
ON Commitments.job_id=Jobs.job_id
WHERE Jobs.Project_Manager LIKE :projectmanager
or if you only want Project_Manager with commitments:
SELECT *
FROM Jobs inner JOIN Commitments
ON Commitments.job_id=Jobs.job_id
WHERE Jobs.Project_Manager LIKE :projectmanager
Try the following:
SELECT * FROM
Jobs j INNER JOIN Commitments c on j.job_id = c.job_id
WHERE j.project_manager LIKE :projectmanager
Imagine a table for articles. In addition to the main query:
SELECT * From articles WHERE article_id='$id'
We also need several other queries to get
SELECT * FROM users WHERE user_id='$author_id' // Taken from main query
SELECT tags.tag
FROM tags
INNER JOIN tag_map
ON tags.tag_id=tag_map.tag_id
WHERE article_id='$id'
and several more queries for categories, similar articles, etc
Question 1: Is it the best way to perform these queries separately with PHP and handle the given results, or there is way to combine them?
Question 2: In the absence of many-to-many relationships (e.g. one tag, category, author for every article identified by tag_id, category_id, author_id); What the best (fastest) was to retrieve data from the tables.
If all the relationships are one-many then you could quite easily retrieve all this data in one query such as
SELECT
[fields required]
FROM
articles a
INNER JOIN
users u ON a.author_id=u.user_id
INNER JOIN
tag_map tm ON tm.article_id=a.article_id
INNER JOIN
tags t t.tag_id=tm.tag_id
WHERE
a.article_id='$id'
This would usually be faster than the three queries separately along as your tables are indexed correctly as MySQL is built to do this! It would save on two round trips to the database and the associated overhead.
You can merge in the user in the first query:
SELECT a.*, u.*
FROM articles a
JOIN users u ON u.user_id = a.author_id
WHERE a.article_id='$id';
You could do the same with the tags, but that would introduce some redundancy in the answer, because there are obviously multiple tags per article. May or may not be beneficial.
In the absence of many-to-many relationships, this would do the job in one fell swoop and would be superior in any case:
SELECT *
FROM users u
JOIN articles a ON a.author_id = u.user_id
JOIN tag t USING (tag_id) -- I assume a column articles.tag_id in this case
WHERE a.article_id = '$id';
You may want to be more selective on which columns to return. If tags ar not guaranteed to exist, make the second JOIN a LEFT JOIN.
You could add an appropriately denormalized view over your normalized tables where each record contains all the data you need. Or you could encapsulate the SQL calls in stored procedures and call these procs from your code, which should aid performance. Prove both out and get the hard figures; always better to make decisions based on evidence rather that ideas. :)