PDO Two Table Joining - php

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.

Related

I dont know whats wrong with my SQL join, Its my fisrt time using it

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.

Find smallest and largest linked values in a many-to-many table relationship

I am building a chorus management database and need to create a particular query. (MySQL programming in PHP.)
I have a table of singers and a table of events which have a many-to-many relationship managed by a roster table. Each roster record links to one SingerID and one EventID. I would like to create a browse table of the form:
The catch is that some singers may have no events linked.
Is there a way to do this in a single MySQL query, or will I need to write one query to list all my singers, and a second query to list all of the events for each singer, and then examine the second query to extract the first and last records (assuming I sort the last query by date)?
Use a LEFT JOIN that way it doesn't need to exist
I also think the way Mike does, Query should be like this
SELECT * FROM singer
LEFT OUTER JOIN roster ON singer.id =roster.singerid
INNER JOIN event ON event.id=roster.eventid
ORDER BY singer.name,singer.date

how to retrieve data without using outer joins, or without using any joins?

How can we retrieving data from multiple tables without using "LEFT JOIN" or "RIGHT JOIN" in mysql?
OR
Is it Possible by using PHP/Mysql?
some of the case i can retrive data. such as when we use "two tables" or there have no complex "WHERE" condition.
But when i use 5,6,7----15 tables that case it is impossible for me to retrive data.
Please can any one help me.
Thanks Advance.
Do a search on the first table,
On the second table do a SELECT * FROM table_2 WHERE table_2.fk_table_1 IN ($pks_from_table_one)
and go on ... but this means you will do n queries, with n round trips to the DB - I've heard that some companies forbids JOINs and sometimes it is indeed better - but I didn't do it yet and don't recommend it either.
Let's say you have foods and people. You want to get the foods people like.
foods: id,food_name
people: id,name,food_id
food_id here refers to the id field in the foods table.
Simply do:
SELECT people.name,food.food_name FROM people,foods WHERE people.food_id=foods.id
You can use "alias" instead of "Join".
Assume you have 4 table.
Syntax:
Select * from Table1 A, Table2 B, Table3 C, Table4 D
where A.id= B.id and A.city=C.city and B.subject=D.subject
You can make this by adding a subquery without using any JOIN statement, in the example I gave you three tables were used to extract Users that are our customers and their mount is less or equal than 999, check it out..
Select TUsers.names TCustomers.names
FROM TUsers, TCustomers
Where TUsers.id = TCustomer.id AND TCustomers.id IN
(Select TCustomer.id
FROM TCustomer
WHERE TCustomer.amount >= 999)
Rate if it helps :)

Getting SQL result as nested array in PHP

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.

PHP MySql query 2 tables that have no common attributes at the same time?

I am trying to query 2 tables in a database, each query having nothing to do with each other, other then being on the same page.
Query 1 - The first query on the page will retrieve text and images that are found throughout the page from Table A.
Query 2 - The second query will retrieve several products with a image, description and title for each product from Table B.
I know that putting the second query inside the first query's while loop would work but of course is very inefficient.
How can I and what is the best way to retrieve all the data I need through 1 query?
Thanks,
Dane
So all you want to know is if its ok to have 2 queries on the same webpage? Its A-OK. Go right ahead. Its completelly normal. No one expects a join between table news and table products. Its normal to usetwo queries to fetch data from two unrelated tables.
Use LEFT or INNER JOIN (depends on whether you want to display records from TableA that have no correspondent records in TableB)
SELECT a.*, b.*
FROM TableA a
[LEFT or INNER] JOIN TableB b ON (b.a_id = a.id)
If there's no way to relate the two tables to each other, then you can't use a JOIN to grab records from both. You COULD use a UNION query, but that presumes that you can match up fields from each table, as a UNION requires you to select the same number/type of fields from each table.
SELECT 'pageinfo' AS sourcetable, page.id, page.images, page.this, page.that
WHERE page.id = $id
UNION
SELECT 'product' AS sourcetable, products.id, products.image, product.other, product.stuff
But this is highly ugly. You're still forcing the DB server to do two queries in the background plus the extra work of combining them into a single result set, and then you have to do extra work to dis-entangle in your code to boot.
It's MUCH easier, conceptually and maintenance-wise, to do two seperate queries instead.

Categories