I have two tables, 1st table holds the categories list and 2nd table holds the profile information along with 1st table category id as a foreign key. As follow
Table1
id CATEGORY
1 first
2 second
Table2
id CATEGORY name phone
1 2 John 9999999999
how to retrieve table 2 records along with category name (not id:2 as shown in table 2)
I tried this,
SELECT category, name, phone FROM table2;
I need to see following line as result
second, john, 9999999999
get me out of this step, thanks in advance.
What you need is a JOIN, which means you "combine" two tables by linking rows from one table to rows from another table based on some criterion.
In your case, the criterion is that the value of CATEGORY in table2 must equal the value of ID in table1, which is expressed as follows:
SELECT table1.category,
table2.name,
table2.phone
FROM table2
JOIN table1
ON table2.category = table1.id
If needed, you can add a WHERE clause to limit the result to specific rows, e.g. WHERE table1.id = 9999999999 would filter the rows that have category 9999999999.
This should work:
SELECT t1.category, t2.name, t2.phone
FROM table2 AS t2
JOIN table1 AS t1
ON t1.id = t2.category
You can make an INNER JOIN and get the category from the Table 1, like this:
SELECT tb1.category, tb2.name, tb2.phone
FROM table2 tb2
INNER JOIN table1 tb1 on tb2.category = tb1.id
WHERE tb2.id = 1;
Hope it helps!
Related
See sample data below.
Here is what I have so far.
SELECT
table1.stuff,
COUNT(table1.id) AS row_count,
COUNT(comment_table.id) AS comment_count
FROM table1
LEFT JOIN comment_table
ON table1.id = comment_table.page_id
AND comment_table.page_type = 'nofun'
UNION
SELECT
table2.stuff,
COUNT(table2.id) AS row_count,
COUNT(comment_table.id) AS comment_count
FROM table2
LEFT JOIN comment_table
ON table2.id = comment_table.page_id
AND comment_table.page_type = 'fun'
Sample data table1:
id stuff
1 the stuff
2 other stuff
Sample data table2:
id stuff
1 the stuff still
2 other stuff still
Sample data comment_table:
id page_id page_type comment
5 1 fun Ello
6 2 nofun hey janis
Desired Results:
stuff comment_count row_count
the stuff still 1 2
other stuff 1 0
logic of sample data:
I would like to return all rows from table1 and table2. I would also like to get the total number of comments from comment_table associated with each row from table1 and table2 and lastly, count the total number of row returned from table1 and table2.
table1 and table2 have a "page_type", in this case table1 is "nofun" and table2 is "fun". Each comment_table row is related to either table1 or table2 by it's "page_type" AND "page_id", row id of either table1 or table2.
table1 id:2 has one comment (comment_table id:6) and table2 id:1 has one comment (comment_table id:5).
The desired results table shows two sample rows with 'stuff' from table1 and table2 and also the comment count for each row, and a total number of rows.
I do not believe I am wholly understanding the left join. How can I accomplish my goal.?
BIG thanks to #Solarflare for pointing out the lack of GROUP BY.
Here is what I needed:
SELECT
`table1`.`stuff`,
COUNT(`comment_table`.`id`) AS comment_count
FROM `table1`
LEFT JOIN `comment_table`
ON `table1.id` = `comment_table`.`page_id`
AND `comment_table`.`page_type` = 'nofun'
GROUP BY `table1`.`id`
UNION
SELECT
`table2`.`stuff`,
COUNT(`comment_table`.`id`) AS comment_count
FROM `table2`
LEFT JOIN `comment_table`
ON `table2`.`id` = `comment_table`.`page_id`
AND `comment_table`.`page_type` = 'fun'
GROUP BY `table2`.`id`
Data Results:
stuff comment_count
the stuff still 1
other stuff 1
I removed "row_count" since the data is properly grouped I can get the total row count from $page->num_rows.
Thank you everyone who took their time to view my question and certainly those who took the time to comment.
Please find picture to get more clarification on what I am asking
Click here
Let me tell you full description
I have two tables -
Table1 contains book_id and book_name
second table -
Table2 contains book_id, stock and cs_id
I want when someone choose cs_id so result displayed in terms of book_name and not book_id
I'm sorry for short description...
I have two tables in mysql database -
table1
SN ID Name
1 E-11 ELC
2 E-13 ELX
3 D-41 DME
and table2
SN ID CS_ID
1 E-11 C01
2 E-13 C01
3 D-41 C54
How to get result of all name from table1 using one cs_id from table2 using php.
Please help.
Thanks in advance!
Here what I'm using
$query = "SELECT table1.id, table2.name FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE table1.cs_id=$cs_id'
this is showing me 0 result.
Use TABLE LEFT JOIN
LIKE
select tb1.name,tb2.ID FROM TABLE1 as tb1 LEFT JOIN TABLE2 as tb2 ON tb1.ID=tb2.ID where tb2.CS_ID='$request_id';
I hope its work
I have two tables. Both have fields called "ID".
Table 1 has "ID", "Title", "Shift"
Table 2 has "ID", "Table1ID", "Details"
I would like to query Table 2 and retrieve all it's details based on an "ID" value but also get the values from Table 1 that relate to the "Table1ID" value.
I've tried this...
SELECT * FROM Table2 a, Table1 b WHERE a.TableID = b.ID
This works but only retrieves one tables "ID" field.
I've been playing about with UNION ALL but can't get it to work.
Any ideas?
Thanks
Yes, you can add an alias:
SELECT a.ID AID, a.Title, a.Shift, b.ID BID, b.TableID, b.Details
FROM Table2 a, Table1 b
WHERE a.TableID = b.ID
The above will return ID from table A and B as AID and BID in the result.
Is there a way to get the SELECT to get all the fields (without
explicitly writing them) and still alias a specific field?
yes its possible but is considered harmful
This is how you would do id
SELECT Table1.*,Table2.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
Why I'm saying its harmful see here : Why is SELECT * considered harmful?
Proper select should be :
SELECT Table1.ID,Table1.Title,Table1.Shift,Table2.ID.Table2.Table1ID,Table2.Details.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
I need to query the database to pull all cars that a particular user likes.
I know that user_id is 1
Then I have 2 tables. 1 contains all the cars ... id and description and table 2 contains the likes.
Table 1 has a list if cars and these fields:
car_id
car_name,
car_description
Table 2 has what cars I like and these fields:
user_id
car_id
likes (1 or 0)
So I need to pull out only the records that user 1 likes from Table 2 but only the ones he likes
What SQL query would I need to do for that?
SELECT * FROM table1 as t0
LEFT JOIN table2 as t1 on t0.car_id = t1.car_id
WHERE t1.likes = 1
Try this
SELECT * FROM table1 as t1 LEFT JOIN table2 as t2 on t1.car_id = t2.car_id WHERE t2.user_id = $user_id
Try this query
SELECT t1.*,t2.*
FROM tbl1 t1,tbl2 t2
WHERE likes = 1 AND user_id = 1 AND t1.car_id = t2.car_id
I have two tables.
Structure of first table:
id
secondtableid
value
Structure of second table:
id
title
How can I select fields 'id' from first table know value of 'value' column and order result by value of 'title' column of second table, if secondtable id is id of second table?
You can order by fields you don't select.
select FirstTable.id, FirstTable.value
from FirstTable
inner join SecondTable on (FirstTable.SecondTableID=SecondTable.ID)
order by SecondTable.Title
:)
select id from first_table
inner join second_table on first_table.secondtableid = second_table.id
where value = 'Known Value'
order by second_table.title
Something like this:
select
firsttable.id, firsttable.value, secondtable.title
from
firsttable
join
secondtable on firsttable.secondtableid = secondtable.id
order by
secondtable.title
This a beginner question, so I will answer as simple as possible:
SELECT t1.id, t1.value
FROM table1 t1
INNER JOIN table2 t2
ON t1.secondtableid = t2.id
ORDER BY t2.title
You can JOIN the tables, indicating how they join.