Find duplicate value accross multiple column within same row in Mysql - php

Please help find duplicate entries in multiple columns of same row in MySQL:

If you want to find the records that have duplicates in columns you can use this query:
SELECT T1.* FROM tbl T1
JOIN
(SELECT id
FROM (
SELECT id, sample1 AS n from tbl
UNION ALL
SELECT id, sample2 AS n from tbl
UNION ALL
SELECT id, sample3 AS n from tbl
) AS X
GROUP BY id, n
HAVING COUNT(*) > 1
) T2
ON T1.id = T2.id;
You can also test it Here

I am not sure with MySql,
But in PHP below example will use.
Example
$query = "select * from table_name";
$result = mysqli_query($query);
while ($row = mysqli_fetch_array($result)) {
if ($row['sample1'] == $row['sample2']) {
// This row duplicate
}
else {
// This row not duplicate
}
}

SELECT Sample1, COUNT(*) C FROM tablename GROUP BY Sample1 HAVING C > 1;
Finding duplicate values in MySQL
MySQL select records for duplicates using multiple columns

Related

php/mysql Merge 2 tables with same Columns correctly

EDIT: Thanks For JoseCarlosPB here is the solution:
SELECT
(select count(DISTINCT userscount) AS duplicates from users1)
+
(select count(DISTINCT userscount) AS duplicates from users2) AS duplicates
FROM dual;
So i have 2 tables and a mysql statement i want to merge 2 tables but there is same column in both.I think There is nothing to do in PHP code i think the problem is in the mysql statement.
my code works(no errors) but it gives unexpected output.i simply want to correctly count userscount here is what i mean:
table1 has 32 users and table2 has 44 users
i thought the output will be 76 users but it is 3244
PHP:
include 'conn00.php';
$sql = "select DISTINCT userscount, count(DISTINCT userscount) AS duplicates from users1 UNION ALL
select DISTINCT userscount, count(DISTINCT userscount) AS duplicates from users2";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["duplicates"];
}
} else {
}
table1:
users1
32
table2:
users2
44
output: 3244 i want the output to be 76
If the solution is in mysql will be better bcz i have more similar sql statements and i want apply the solution for all of it.
thanks for answering
I think you can do it this way, almost like alistaircol said
SELECT
(select count(DISTINCT userscount) AS duplicates from users1)
+
(select count(DISTINCT userscount) AS duplicates from users2) AS duplicates
FROM dual;
Please check article you need Combine two MYSQL table with same column Name
Just comparing to this you need to move count outside of UNION in order to work as you expected
Maybe you could try something like this for your query:
SELECT
(select count(DISTINCT userscount) AS duplicates from users1 group by usercount)
+
(select count(DISTINCT userscount) AS duplicates from users2 group by usercount)
FROM dual;
DUAL is purely for the convenience of people who require that all SELECT statements should have FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does not require FROM DUAL if no tables are referenced.
More info about DUAL: https://dev.mysql.com/doc/refman/8.0/en/select.html
maybe you can change your code query into this to sum the entire counting value
include 'conn00.php';
$sql = "select sum(i) as duplicates from (
select count(DISTINCT userscount) as i from users1 union all
select count(DISTINCT userscount) as i from users2
) x";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["duplicates"];
}
} else {
}
I am not sure, if the semantics of the query was correct, but the syntax must be like this (WHERE clause goes after JOIN and ON):
SELECT users1.text1, users2.name,users2.email,
users1.username
FROM users1
INNER JOIN users2
ON users1.text1=users2.text1
WHERE users1.username = $_SESSION[username]
SELECT * from
( (select count(DISTINCT userscount) AS duplicates from users1)
union all
(select count(DISTINCT userscount) AS duplicates from users2)
) as duplicates
You can also merge in this way.hope it was helpful

how to generate sql query for the summation of specific column and then rank it

i have a table mark_summery, Here is my table structure
now i just want to sum the "obt_marks" and "total_marks" of specific student with multiple exams (maximum four). "multiple exams mean summation of four exams " how can i sql it. any idea.?
Try using this code
select s.StudentID, s.name, sum(marks)
from tbl_marks m inner join tbl_student s
on m.studentid=s.studentid
group by s.studentid
having sum(marks)=
(
select max(tbl.totalmarks) from
(select studentid, sum(marks) as totalmarks from tbl_marks group by studentid) tbl
)
Try using this code
You could use sum group by on a subqyery for the number of exam for each student
select student_id, sum(obt_marks), sum(total_marks)
from my_table
inner join (
select student_id, count(exam_id) num_exam
from my_table
group by student_id
) t on t.student = my_table.student_id and t.num_exam <= 4
group by student_id
You could create a metod which fetches the data and into you're controller you could sum it with a foreach
$user = $this->User_model->total_activity($id);
$sum = 0;
foreach ($user as $key) {
if ($key['db_field'] !== null) {
$sum += $key['db_field']);
}
}
return $sum;

how detect result is from which table, when use select ... union ... in sql

$result = mysql_query( "SELECT id FROM table1 UNION ALL SELECT id FROM table2;" , $link);
while($end = mysql_fetch_assoc($result))
{
echo $end['id']; // how separate and specify output is frow which table ?
}
i have two table (phpmyadmin), and i want to search in both of them with 1qeury, then i'm use UNION to combine, now i want to seprating output and specify from what table is evry result (row)? tnx
Add an extra column that indicates which table it is:
SELECT "table1" which, id FROM table1
UNION ALL
SELECT "table2" which, id FROM table2
Now you can use $end['which'] to know which table each row came from.
which isn't a column in the table itself. It's an alias for the literal string that's just in the query.
To get the results ordered alternating between the tables, you can do:
SELECT *
FROM (
SELECT "table1" which, id FROM table1
UNION ALL
SELECT "table2" which, id FROM table2
) x
ORDER BY id, which
Use aliases:
$result = mysql_query( "SELECT id as table1_id FROM table1
UNION ALL SELECT id FROM table2;" , $link);
And then:
echo $end['table1_id'];
echo $end['id'];

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`

php sql count from 3 tables

I need to get a count from a table, based on data from 2 other tables.
This is my tables structure:
table1 (id, name)
table2 (id, a, b, c)
table3 (id, blah)
Can i do it all in one statement? Something like this:
SELECT count(*) from table3 WHERE table2.x=table1.name
The hard part is the x column is the name of 'table1.name'. So i dont actually know what x is when im running the statement.
This makes me think i'd have to run a statement to find the name of x before i run this one.
Or... maybe some JOIN?
CURRENT CODE WHICH I USE:
if ($rs[firearm] != "") {
$sql_result2 = mysql_query("SELECT * FROM db_firearms WHERE name='$rs[firearm]'", $db);
$rs2 = mysql_fetch_array($sql_result2);
$sql_result3 = mysql_query("SELECT * FROM items_firearms WHERE player='$id'", $db);
$rs3 = mysql_fetch_array($sql_result3);
if ($rs3[$rs2[shortname]] < 1) {
mysql_query("UPDATE players SET firearm = '' WHERE id ='$id'");
}
}
Yeah, sounds like joins will be necessary.
SELECT count(*) AS num_rows
FROM table3
LEFT JOIN table1.name ON table1.name = table3.name
INNER JOIN table2 ON table2.x = table1.name

Categories