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
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.
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!
Hello i searched on google but i could not find it(maybe wrong search terms) But i'm asking if there is a way to check if name1 from table 1 exists in table 2
So like
select name from table 1.
search in table 2 for the name from table 1
is this possible? if yes how?
~Kev (bad english = sorry)
Select name from table1 Inner Join table2 on table1.name = table2.name;
Depending on your structure this will give you all the names which exists both in table1 and table2 since the joining is done itself on the name
Perhaps something like this (untested)
SELECT name1 FROM tableA WHERE name1= (SELECT name2 FROM table2 WHERE .... )
You are asking about joins between 2 tables. To query all entries from table 1 that exists in table 2 you need next SQL:
SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.name = t2.name
$s2="select * from trackyesttrackyest";
$q2=mysql_query($s2) or die($s2);
$row=mysql_fetch_array($q2);
$s="select * from <secondtablename> where rsname='".$row['rsname']."'";
$q=mysql_query($s) or die($s);
$row2=mysql_fetch_array($q);
please refer this link
click here
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Its simple buddy.........
try out this...
SELECT * FROM table1 WHERE table1.name in (SELECT table2.name FROM table2)
I have two table one table containing station id and station name, and another table containing id, name, startStationId, endStationId. I know second table's id by using that id i need to fetch all details of second table with station name for corresponding startStationId, endStationId.
ex: table1
---------------------------------
slNo staionId staionName
---------------------------------
1 0012 Bangalore ,
2 0014 Chennai ,
3 0015 Mumbai
---------------------------------
Table 2:
------------------------------------------
Id Name startStationId endStationId
-------------------------------------------
123 Dhinesh 0014 0015
-------------------------------------------
For example i know second table id(123).. so i want to fetch all results by using id, result would be.
Id =>123, Name => Dhinesh, StartStaion =>Chennai , Endstation=>Mumbai.
How can we write in one single query...?
Thanks in advance.
Try this.
SELECT t2.Id,t2.name,t1.StationName,t3.StationName
FROM table2 t2
INNER JOIN table1 t1 ON t2.startStationId = t1.stationId
INNER JOIN table1 t3 ON t2.endStationId = t3.stationId
SELECT t2.Id, t2.Name, tstart.stationName , tend.stationName
FROM table2 as t2
INNER JOIN table1 as tstart ON t2.startStationId = tstart.stationId
INNER JOIN table1 as tend ON t2.endStationId = tend.stationId
this should work
I have to three table I need to gather data from in a search process:
Commissions Table - Table1: [affiliate_id]
Affiliates Table - Table2: [id][user_id]
Profiles Table - Table3: [ID][NickName]
The search input I'll have is someone searched for a username. I need to return the data from table 1 where the affiliate_id matches the user_id of Table2, that is like the nickname that will be searched for.
I hope that makes sense :)
Try this:
"select table1.* from table1
inner join table2 on table2.user_id = table1.affiliate_id
inner join table3 on table3.id = table2.user_id
where table3.nickname like '%".mysql_real_escape_string($searchtext)."%'"
SELECT t1.*, t3.nickname FROM Table1 t1
JOIN Table2 t2 ON t2.id=t1.affiliate_id
JOIN Table3 t3 ON t2.user_id=t3.user_id
WHERE t2.user_id=?;