Yevgeniy Bagackiy Yevgeniy Bagackiy - 27 days ago 10
MySQL Question

How to echo only one parameter with union all

I have to database tables. I want to print union all names from both tables and if there is more than one name appears then print it only once and make it red for example.
My PHP:

<?php
include 'verification/verify_form_details.php';

$result5 = getUsers();
if (!$result5) {
die('Invalid query: ' . mysqli_error());
}
while($row5 = mysqli_fetch_array($result5))
{
$username = $row5['username'] ;
echo '<ul>';
echo"<li>". $username ."</li>";
echo '</ul>';
}
?>


And my query:

function getUsers()
{
global $con;
$sql = "SELECT username FROM `user_project`
UNION ALL
SELECT username FROM `user`";
return mysqli_query($con,$sql);
}


How Can I achieve this?

Ima Ima
Answer

Modify your query to group by user name

    $sql = "SELECT username FROM 
            (
            SELECT username FROM `user_project` 
            UNION ALL 
            SELECT username FROM `user`
            )
            GROUP BY username";

Also if you want to get number of appearance so that you can make red those having multiple occurrence

$sql = "SELECT username, count(username) FROM 
                (
                SELECT username FROM `user_project` 
                UNION ALL 
                SELECT username FROM `user`
                )
                GROUP BY username";