I've a ('courses') table that has a HABTM relationship with ('instructors') table through another table...
I want to get the data of an instructor with all related courses in one query..
Currently, I have the following SQL:
SELECT *
FROM `instructors` AS `instructor`
LEFT JOIN `courses` AS `course`
ON `course`.`id` IN (
SELECT `course_id`
FROM `course_instructors`
WHERE `course_instructors`.`instructor_id` = `instructor`.`id`
)
WHERE `instructor`.`id` = 1
This SQL does what it should be doing, the only "problem" I have is that I get multiple rows for each joined rows.
My question is:
Can I get the result I want in one query? Or do I have to manipulate the data in PHP?
I'm using PHP and MySQL.
Each record of a query result set has the same format: same number of fields, same fields, same order of fields. You cannot change that.
SELECT *
FROM instructors AS instructor
LEFT JOIN
course_instructors
ON
instructor.id= course_instructors.instructor_id
LEFT JOIN
courses
ON
course_instructors.course_id = course.id
WHERE instructor.id = 1
This assumes the PK of course_instructors is (instructor_id,course_id)
Explanation of query:
First join + WHERE make sure you get the relevant instructor
Second join matches ALL the entries from the course_instructor table that belongs to this instructor. If none found, will return one row with NULL in all fields
Last join matches all relevant courses from the entries found from course_instructor If none would will return one record with NULL in all fields.
Again: important to use the right constraints to avoid duplicate data.
That's the nature of relational databases. You need to get the instructor first and then get the related courses. That's how I would do it and that's how I've been doing it. I'm not sure if there is a "hack" to it.
Related
I'm trying to make a query to extract elements from 2 tables, which are linked via another table.
So I have 3 tables:
authors
- id, name, book
category
- id, name, description
category-author
- id, idauthor, idcategory
Now I want to make a query to make the following output:
row: authors.id, authors.name, authors.book, category.name
I don't know what category's are linked using the 2 tables only, so I need to use the last one, the category-author table. The id's of both the author and the category are linked via that table.
I got the following query:
SELECT authors.id, authors.name, authors.book, category.name FROM category, author LEFT JOIN SELECT ??
I'm stuck at the remaining part of the query.
Also when I have this query, can I just extract a CSV with phpmyadmin?
You can get related information from different tables using table joins. Relations between tables should be specified using foreign keys (i.e. the column idcategory from category-author is presumably a foreign key that refers to primary key column category.id). In a join clause, you merely specify which tables are to be joined and on what column:
SELECT table1.col1, table2.col2
FROM table1
JOIN table2 ON table1.pkCol = table2.fkCol
This means you can't specify any SELECT or FROM clauses within a JOIN clause. The columns you wish to select from joined tables are all specified in the initial SELECT statement, and you only specify one table in the FROM clause, from which you subsequently perform the table joins.
In your case, I think this should get you started:
SELECT authors.id, authors.name, authors.book, category.name
FROM category
LEFT JOIN category-author ON category-author.idcategory = category.id
LEFT JOIN authors ON authors.id = category-author.idauthor
I'm not sure how familiar you are with foreign keys, primary keys and table joins, so I won't elaborate any more on this. I think specifying multiple tables in a FROM clause is bad practice, even if your database system still supports it (Related question).
From then on, you can easily export the results from within PhpMyAdmin, as there is an export button for every table overview, including query results.
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.
Ok, I have this code, I dont have error.... i have just nothing.... nothing apears:
$idea = $bdd->query("SELECT * FROM ideas
INNER JOIN follow ON ideas.idcreador=follow.idseguidor
WHERE follow.idseguidor ='".$_SESSION['userid']."' ORDER BY id DESC");
while($datoideaperfil2 = $idea->fetch())
{
echo $datoideaperfil2['ideas.idcreador'] <br />;
}
What is wrong? Help me please its my fist time with SQL Joins...
Thanks
There doesn't appear to be anything wrong with the SQL itself, but one thing that I do suspect is that you have a column called id in both tables. If you don't use an alias, then mysql won't know what to order by and return an error.
Try this:
SELECT
*
FROM
ideas
INNER JOIN follow
ON ideas.idcreador=follow.idseguidor
WHERE
follow.idseguidor ='".$_SESSION['userid']."'
ORDER BY
follow.id DESC
To understand what's wrong with the query you need to understand how joins in SQL work. A JOIN results in a "table" where for every row in table A, you get a resulting row that is combined with every row in table B. So if table A has 5 rows, and table B has 10 rows, you get 50 (5x10) rows, which you need to filter with your WHERE clause.
Results also differ, and in your case this is the important part, wether you go for an INNER JOIN or an OUTER JOIN. An INNER JOIN's resulting "table", ONLY contains rows where the rows from table A and table B match the ON clause. So if you have a row in table ideas, which has no relation to any rows in table follow, it won't show up in the results. Would you choose an OUTER JOIN (a LEFT or RIGHT OUTER JOIN) any non-matching row will show up, but the row's values will be set to NULL for whatever values it could not find a relation for.
Since you chose an INNER JOIN, and you get no results, I'm guessing you have no relations between any rows.
What's also important is that you specify what table you want the ORDER BY clause to be ordered by, or you get an ambigious column exception (if both tables have a column named id).
you have used a INNER JOIN which will only display data if both tables have corresponding rows. Try using a LEFT or RIGHT join.
I'm trying to build a review record based on fields from 5 tables:
I've marked all the columns I need, but for the moment I'm just retrieving all of the user_rating table.
Here's what I have so far:
SELECT DISTINCT user_rating.*, whiskey.name, user_notes.overall, users.image, user_rate.rate_number
FROM user_rating
LEFT JOIN whiskey ON whiskey.id = user_rating.whiskeyid
LEFT JOIN users ON users.username = user_rating.username
LEFT JOIN user_notes ON user_notes.username = user_rating.username AND user_rating.whiskeyid = user_notes.whiskey_id
LEFT JOIN user_rate ON user_rate.whiskey_id = user_rating.whiskeyid AND user_rate.username = user_rating.username
ORDER BY user_rating.id DESC
At first I thought this was giving me the results I wanted but then I noticed I was getting multiple rows as well as too many null fields. Any help would be greatly appreciated.
Edit:
By multiple rows I mean duplicate rows. Also, I am aware that a left join produces null values on the right side of the join. What I meant to say is that I'm getting more null values than I should be as the data is within the database.
To clarify, I'm trying to create a list of recent reviews with the most recent listed first. Each review consists of a username, 11 categories (each one is an integer value), overall rating (int value), notes (string), image (URL), and whiskey name (string).
1) You can't be getting multiple SAME rows, since you use DISTINCT. (by multiple do you mean duplicate?)
2) You get null fields because you are using LEFT JOIN and your tables cannot be joined (some "ON clause" cannot be evaluated as true)
It turns out that there was nothing wrong with my query. The person whose database I'm using isn't maintaining it so there are several reviews for the same product and user where only the overall rating is different. Also, there are many reviews on products that don't even exist. I should've looked at the database closer to begin with as deleting all of the erroneous rows solved my problem. Thanks for all the input.
I have two mysql tables that I am querying together and the query works fine. The tables are car and requests
The problem is that i need both the auto increment id's and it only gives me one.
The one it gives me is not the joined tabled.
Here is my query so far
SELECT * FROM `car` INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id` WHERE `car`.`user_id` =21
I just need to someway get the auto increment id of the requests table.
As always stack exchange is the best place to come for answers so thanks in advance!
Just specify your query as this and you'll get both but with different names:
SELECT `car`.id AS car_id, `requests`.id AS request_id, *
FROM `car`
INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id`
WHERE `car`.`user_id` =21
Note that you will still have an ID column which SHOULD be the requests.id, just diregard it and use car_id and request_id...
When you have to join tables and table contains same name than it creates an ambiguous situation. So always use table name or table alias with column name Like this
SELECT t.column
FROM table as t
OR
SELECT table.column
FROM table