Filtering duplicates while joining tables - php

I need to select data from both tables and display it but my PHP file seems to be posting duplicates. I want to be able to pick up all of the descriptions and relate it to the ID's which are used in table1.
I managed to get some of it to work but it displays the name Jake twice is their anyway i can filter out the duplicates.
Thanks
Here is my code
$sql = "SELECT table1.name, table2.description
FROM table1
INNER JOIN table2
on table1.id = table2.customerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['name']."<br>";
echo $row['description']."<br>";
}
table1
id name
1 james
2 jake
3 aaron
4 holly
table2
id customerid description
1 1 hey1
2 2 hey2
3 2 hey3
4 3 hey4
5 4 hey5

you can use a DISTINCT constraint.
If two rows are assumed to be "identical" only if the rows have the same values for all columns, you group them in one DISTINCT query:
SELECT DISTINCT table1.name,table2.description
FROM table1
INNER JOIN table2
ON table1.id = table2.customerid
Otherwise, you can use the GROUP BY statement. But then a problem arises: which "description" will you pick? Since there are two for Jake. MySQL has implemented several function aggregates. A potential candidate is MIN which returns the alphabetically sorted minimum. The query then reads:
SELECT table1.name,MIN(table2.description)
FROM table1
INNER JOIN table2
ON table1.id = table2.customerid
GROUP BY table1.name

Related

count comments for each row Union and Left Join

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.

mysql fetch data from two tables

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

PHP SQL get data from 2 tables and output wanted information

i am trying to achieve the following result. (i am sorry for the horrible explanation but i find it very hard to explain :P)
I need data from 2 tables.
Table1
id, table2_id, user_id
Table2
id, Name
Table1 example information
ID 1 Table2_id 1 user_id 3
ID 2 Table2_id 2 user_id 3
ID 3 Table2_id 5 user_id 3
Table2 Example Information
ID 1 Name TEST
ID 2 Name Hello
ID 3 Name Helpme
ID 4 Name Please
ID 5 Name IamLost
i Would like to output everything tied user_id 3. This would be my ideal end result
TEST
Hello
IamLost
I have this as code
$id = "3";
$sth = $db->prepare("SELECT table2_id, user_id, naam FROM table1, table2 where user_id = $id ");
$sth->execute();
$result = $sth->fetchAll();
foreach( $result as $row ) {
echo $row['table2_id'] . ' ';
echo $row['naam'] . '<br>';
}
But this just outputs everything but then twice. like this
TEST
TEST
Hello
Hello
Helpme
Helpme
Please
Please
IamLost
IamLost
A LEFT JOIN should do the trick:
SELECT `table1`.`table2_id`, `table1`.`user_id`, `table2`.`name`
FROM `table1`
LEFT JOIN `table2`
ON `table1`.`Table2_id` = `table2`.`id`
WHERE `table1`.`id` = $id
MySQL JOIN Syntax
Use Joins in SQL.
The SQL Query should look like this:
SELECT T1.USER_ID, T2.NAME
FROM TABLE1 AS T1
JOIN TABLE2 AS T2
ON T1.TABLE2_ID = T2.ID
WHERE T1.USER_ID = 3
these two table must be related to each other. When you select , returned rows should be equal this two tables. This why we use table joins e.g
SELECT a.user_id,a.table_id,b.name FROM table1 as a, table2 as b
RIGHT OUTER JOIN table 1
ON b.ID = a.table2_id
AND table1.user_id = 3
I believe you just need to be more precise:
$sth = $db->prepare("SELECT table2_id, user_id, name
FROM table1
LEFT JOIN table2
ON table1.Table2_id = table2.id
WHERE user_id = $id ");
A simple right outer join will help you here.
SELECT * FROM table2
RIGHT OUTER JOIN table 1
ON table2.ID = table1.table2_id
AND table1.user_id = 3

getting secondary name using sub query

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!

PHP SQL JOIN Avoid ID Duplicates

I am using this code:
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
Then to count the number of records return:
$num_rows = mysql_num_rows($result);
echo "$num_rows";
It works great but venue_id in Table1 has lots of entries and I only want it to get one per venue_id
How can I make it so it only returns 1 venue_id instance?
Use GROUP BY clause,
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
GROUP BY `Table1`.`venue_id`

Categories