Need mysql query to pull data from two tables

learning_curve

So after helpful feedback from my original question, I now have this query:

SELECT sessions.id, sessions.title, sessions.abstract, sessions.presenters, sessions.proposal_id, proposals.outcomes, proposals.CategorySelection, proposals.research3, proposals.research4, proposals.research5, proposals.research6, proposals.innovation3, proposals.innovation4, proposals.innovation5,proposals.innovation6, proposals.application3, proposals.application4, proposals.application5, proposals.application6, proposals.integration3, proposals.integration4, proposals.integration5, proposals.integration6, proposals.references, proposals.organization
    FROM sessions, proposals
    INNER JOIN proposals ON proposals.id = sessions.proposal_id
    WHERE sessions.id = '$id
    LIMIT 1;)

that is getting me nowhere fast. What am I doing wrong?


Original question: I need to pull several fields from one table and several more from a second table. The criteria is that a field called proposal_id match the id field of the second table. I am fairly new so this is what I have so far. It is not working, but not sure how to make it work.

(SELECT `title`,`abstract`,`presenters`,`proposal_id` FROM `sessions` WHERE   `id`='$id')
    UNION
    (SELECT `outcomes`,`CategorySelection`,`research3`,`research4`,`research5`,`research6`,`innovation3`,`innovation4`,`innovation5`,
        `innovation6`,`application3`,`application4`,`application5`,`application6`,`integration3`,`integration4`,`integration5`,`integration6`,`references`,`organization` FROM `proposals` WHERE `id`= `sessions`.`proposal_id`)
        LIMIT 1;
Abhik Chakraborty

You need to use JOIN not UNION

select 
s.*,p.*
from `sessions` s
inner join `proposals` p on p.id = s.proposal_id
where s.id = '$id'

This is how you can join both the tables using the common key between.

You can select the specific fields instead of .* by specifying the column names as

s.col1,s.col2,p.col1,p.col2 

etc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

need help building one mysql query comparing a variable with two tables

From Dev

Find uncommon data from two tables in mysql

From Dev

mysql query request from two tables with IN clause

From Dev

Selecting data from two tables with sql query

From Dev

Need hint with MySQL query from 2 Tables

From Dev

mysql query to select data from three tables

From Dev

Mysql query for getting values from two tables

From Dev

SQL query: combining data from two tables

From Dev

MySQL Query Of Two Tables

From Dev

mySQL combining data from tables query

From Dev

need to fetch data from 2 tables in mysql

From Dev

Mysql (conditional?) query from two tables

From Dev

mysql query request from two tables with IN clause

From Dev

MySQL selecting from two tables in the same query

From Dev

MySQL Query to retrieve data from two tables even no data found and unique column in both tables

From Dev

mysql query to retrive data from two tables having a common field

From Dev

MySQL query from PHP combining data from two tables

From Dev

Mysql: help in query from two tables

From Dev

MySQL query for taking data from two tables with inner join, aliases

From Dev

mysql query to select data from three tables

From Dev

Pull out data from different tables in mysql

From Dev

SQL query: combining data from two tables

From Dev

mySQL combining data from tables query

From Dev

query Fetch data from multiple tables MySQL

From Dev

SQL SELECT query to pull data from tables to calculate a sum

From Dev

MySQL query in two tables

From Dev

Need MySQL query from three tables

From Dev

sql query to merge data from two tables

From Dev

MySQL Query using data from two tables

Related Related

HotTag

Archive