Fetch many to many relationship table data [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was trying to fetch data from following tables.
table_question
q_id | question
1 | q1
2 | q2
3 | q3
4 | q4
table_answer
a_id| answer
1 | a1
2 | a2
3 | a3
4 | a4
5 | a5
table_entity
e_id| q_id | a_id
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 4
5 | 3 | 5
6 | 4 | 2
I want a question and answer. Please give me some suggestion.
Fetche data by using doctorine 2 ORM.

Try this mysql with your phpmyadmin
$qry = "SELECT quetion.que,answer.ans FROM entity INNER JOIN quetion ON quetion.id=entity.q_id Inner Join answer On answer.id=entity.a_id";
There are use join table for join those table and get the data from those table

you need to use join query. With joining both table you can get all tables relational data
$query = SELECT table_question.question,table_answer.answer FROM table_entity INNER JOIN table_question ON table_question.id=table_entity.`q_id ` Inner Join table_answer On table_answer.id=table_entity.a_id ;

Here you go:
SELECT table_question.question, table_answer,answer FROM table_entity
JOIN table_question ON (table_entity.q_id = table_question.q_id)
JOIN table_answer ON (table_entity.a_id = table_answer.a_id)

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)';

combining different tables in 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 4 years ago.
Improve this question
i have 3 tables which i want to combine in a empty table,
Table A contains:
a_id | name
1 | john
2 | mic
3 | rog
and
Table B contains:
b_id | name
10 | rims
11 | sara
and
Table c contains:
c_id | name
20 | johny
21 | sun
22 | rose
23 | pash
24 | ed
25 | ese
and i have one empty table D, which will have id's of all three above tables:
Table D columns are;
a_id | b_id | c_id
how can i insert all id's in table D? and
when i run query.
Select*from table_D
it should show all id's from table(a,b,c).
Your question is rather vague, because you don't specify what d looks like. Let me assume that you was a Cartesian product of all ids. This seems like a reasonable assumption. Then:
insert into d (a_id, b_id, c_id)
select a.a_id, b.b_id, c.c_id
from a cross join b cross join c;
Here is a rextester demonstrating it.
Here's what you ask for:
INSERT d
SELECT a.id, b.id, c.id
FROM a CROSS JOIN b CROSS JOIN c
Though I doubt it's what you want!

SQL query using data from other table [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 6 years ago.
Improve this question
I have a normalized table:
`Table: TheMovies`
id | MovieName
---------------------
1 | Zootopia
2 | Moana
3 | Toy Story
`Table: TheGenres`
id | GenreName
---------------------
21 | Action
22 | Animation
23 | Adventure
`Table: mMoviesGenres`
movieID | genreID
---------------------
1 | 21
1 | 23
2 | 22
2 | 23
2 | 21
3 | 23
All works fine, but I need a Query which will shoe me similiar movies based on at least one of the genres of MovieID = 1.
Can you give me an sql query so I have a basic idea of doing that, to be able to create more advanced queries?
To query using data from another table, you can join two or more tables into a single table by using JOIN clause.
SELECT TheMovies.* FROM mMoviesGenres JOIN TheMovies ON mMoviesGenres.MovieID = TheMovies.MovieID WHERE mMoviesGenres.MovieId <> 1 AND mMoviesGenres.GenreID IN (SELECT GenreID FROM mMoviesGenres WHERE MovieID = 1)
Learn more about join: Using Join to Retrieve Data from Multiple Tables
SELECT TheMovies.*
FROM mMoviesGenres
JOIN TheMovies
ON mMoviesGenres.MovieID = TheMovies.MovieID
WHERE
mMoviesGenres.MovieId <> 1
AND mMoviesGenres.GenreID IN (SELECT GenreID FROM mMoviesGenres WHERE MovieID = 1)

MySQL - Retrieve data from database by joining 3 tables and display one column value with comma [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 6 years ago.
Improve this question
I have three tables, named location and Hospital and hospital location. These are the fields and data of both table
Table : location
id | location_name
1 | location1
2 | location2
Table : hospital
id | hospital_name
1 | Hospital1
2 | Hospital2
Table : hospital_location
id | hospital_id | location_id
1 | 1 | 1
2 | 1 | 2
I need to create a query in mysql to display all the data from hospital table. The location_name column has multiple values, separated by a comma.
id | hospital_name | location_name
1 | Hospital1 | location1, location2
What you need is the GROUP_CONCAT mysql function
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
SELECT h.hospital_name, GROUP_CONCAT(l.location_name) as location_name
FROM hospital h
LEFT JOIN hospital_location hl ON hl.hospital_id = h.id
LEFT JOIN location.l ON hl.location_id = l.id
GROUP BY h.id

mysql get name from another table with same id [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 think this is a really simple question but I can't crack it.
I have two tables in my mysql database, clubs_db and leagues_db.
clubs_db
id | name
1 | Club1
2 | Club2
3 | Club3
4 | Club4
5 | Club5
6 | Club6
leagues_db
id | team1 | team2 | team3 | team1_name | team2_name | team3_name |
1 | 1 | 2 | 3 | | | |
2 | 4 | 5 | 6 | | | |
All I want to do is insert the relevant club name into leagues_db from clubs_db.
I also want this to happen automatically when the values in leagues_db change.
Thanks if anybody can help me.
It sounds like you would be better served by dropping the teamN_name columns and using a view that joins the two tables together:
CREATE VIEW leagues_with_names AS
SELECT
l.id, l.team1, l.team2, l.team3,
t1.name AS team1_name,
t2.name AS team2_name,
t3.name AS team3_name
FROM leagues_db l
LEFT OUTER JOIN clubs_db t1 ON l.team1 = t1.id
LEFT OUTER JOIN clubs_db t2 ON l.team2 = t2.id
LEFT OUTER JOIN clubs_db t3 ON l.team3 = t3.id;
Then you can SELECT ... FROM leagues_with_names and not have to worry about the details of the join. Note that the view is not a table in itself; it will fetch data from the other two tables automatically. This means that it will be always up to date.
(See a demo of this query.)

Categories