I have two MySQL tables,
student = 150 (total no of rows which represent total no of student)
employer= 230 (total no of rows which represent total no of employer)
FULL CODE
<?php echo mysqli_num_rows($studempl); ?>
$conn = new mysqli ('localhost', 'root', '', 'dashboard');
$studempl = $conn ->query ("SELECT (SELECT COUNT(*) FROM student) + (SELECT COUNT(*) FROM employer) FROM dual");
$tot_studempl = mysqli_num_rows($studempl);
Result: 1
How can i fix this?
I tried to use
SELECT 'student' AS stdID, COUNT(*) FROM student
UNION
SELECT 'employer' AS emplID, COUNT(*) FROM employer*
It displays the result = 2,
I also tried to use:
SELECT (SELECT COUNT(*) FROM student) AS count1,(SELECT COUNT(*) FROM employer) AS count2 FROM dual*
It displays the result = 1,
I think I might have wrong in my code,
how can I get total from student and from employer so i can view the data and display in piechart?
150, 230
I did the sum of the two tables in MySQL with a query like this
SELECT ( (SELECT COUNT(*) FROM scheme.student) +
(SELECT COUNT(*) FROM scheme.employer)
) AS 'Column' ;
In my case, i used the same table 2 times and that table has 280 rows (280 x 2 = 560).
Hope this help you
You're second query should work.
SELECT (SELECT COUNT(*) FROM student) AS count1,(SELECT COUNT(*)
FROM employer) AS count2 FROM dual* <-- make sure you remove (*)
// so it should be like this
SELECT (SELECT COUNT(*) FROM student) AS count1,(SELECT COUNT(*)
FROM employer) AS count2 FROM dual
Then you just have to fetch the array to display the results
$sql = "SELECT (SELECT COUNT(*) FROM playlists) AS count1,(SELECT COUNT(*) FROM artists) AS count2 FROM dual";
if ($result=mysqli_query($con,$sql))
{
$row = mysqli_fetch_array($result);
var_dump($row); // just checking the raw array...
echo "<br />";
echo $row[0]. ", ". $row[1]; // got your results -> 150, 230
}
mysqli_close($con);
Also, you're first query works if you replace the + to , like this
$sql = "SELECT (SELECT COUNT(*) FROM students), (SELECT COUNT(*) FROM employer) FROM dual";
Related
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
I have a table with player and status, this table not got corrupted and it has duplicate entries like this:
player_id status
----------------------
1 100
2 100
2 101
3 100
1 101
2 101
As you can see this record is duplicate: 2,101
How can I find such records using a mysql query?
I tried:
select * from player group by player_id, status having count(status) > 1 but it did not help.
You can SELECT duplicate records as like that:
SELECT COUNT(*) as total,player_id,`status`
FROM `player`
GROUP BY player_id,`status`
HAVING total > 1;
Do it in a outer query like
select * from (
select *, count(*) as rowcount
from player
group by player_id, status ) tab
where rowcount > 1
You can try this query.
$qrys = <<<QUERY
select s.id, t.*
from player s
join (
select player_id, status, count(*) as qty
from player
group by player_id, status
having count(*) > 1
) t on s.player_id = t.player_id and s.status = t.status
QUERY;
If you want to delete duplicate record then use this script
$delres= mysql_query($qrys);
$player_id = 0;
$status = 0;
while ($r= mysql_fetch_array($delres)){
if($player_id == $r['player_id'] && $status == $r['status'])
mysql_query("DELETE FROM player WHERE id=".$r['id']);
$player_id = $r['player_id'];
$status = $r['status'];
}
Try this :
select * from player p
inner join ( select player_id, count(*) as duplicate
from player group by player_id
having count(*) > 1) pp
on p.player_id = pp.player_id
Try This
SELECT *, COUNT(status) AS rows_count FROM player
GROUP BY player_id, status HAVING COUNT(status) > 0
You query is near to it just some changes in it to give you count column for every options Try this modified query.
select *, count(status) as count from player
group by player_id, status having count(status) > 0
It will give this type of response
you can do that by
SELECT distinct player .*
FROM player as tb1
join player as tb2
WHERE tb1.status = tb2.status
and tb1.player_id < tb2.player_id
You can do it simply with GROUP BY like this
SELECT `player_id`, `status`, COUNT(*) as cnt FROM `player`
GROUP BY CONCAT(`player_id`, '_', `status`) HAVING cnt > 1
result would be
player_id status cnt
2 101 2
I have five different SQL queries that I would like to return back the number of rows returned in a list. Basically I have a MySQL database and would like to show some statistics on how many records we have and how many of them have had information added. Examples of the queries are...
SELECT * FROM `ibf_ccs_custom_database_1`
SELECT * FROM `ibf_ccs_custom_database_2`
SELECT * FROM `ibf_ccs_custom_database_3`
SELECT * FROM `ibf_ccs_custom_database_1` WHERE `field_30` <> ''
SELECT * FROM `ibf_ccs_custom_database_2` WHERE `field_60` <> ''
Which should return back the following output
Total Records Database 1: 12,548
Total Records Database 2: 9,835
Total Records Database 3: 5,916
Filled Out Records in Database 1: 567
Filled Out Records in Database 2: 681
Any help is much appreciated!
It would be much more efficient to have the database prepare a result containing just the count of rows, rather than preparing a complete set of ten thousand plus rows.
For example, to have the database return the results as shown in the question:
SELECT CONCAT('Total Records Database 1: ',FORMAT(COUNT(1),0) AS txt
FROM `ibf_ccs_custom_database_1`
UNION ALL
SELECT CONCAT('Total Records Database 2: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_2`
UNION ALL
SELECT CONCAT('Total Records Database 3: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_3`
UNION ALL
SELECT CONCAT('Filled Out Records in Database 1: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_1` WHERE `field_30` <> ''
UNION ALL
SELECT CONCAT('Filled Out Records in Database 1: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_2` WHERE `field_60` <> ''
If you want to handle the labeling and formatting in the application layer, you could just return the counts in a single row:
SELECT t1.cnt AS t1_cnt
, t2.cnt AS t2_cnt
, t3.cnt AS t3_cnt
, f1.cnt AS f1_cnt
, f2.cnt AS f2_cnt
FROM ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_1` ) t1
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_2` ) t2
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_3` ) t3
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_1` WHERE `field_30` <> '' ) f1
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_1` WHERE `field_60` <> '' ) f2
I'd just build an array with key => value pairs for your row counts. Something like this:
$db_queries = array();
$query = "SELECT * FROM ibf_ccs_custom_database_1";
$res = $mysqli->query($query);
$db_queries['q1'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_2";
$res = $mysqli->query($query);
$db_queries['q2'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_3";
$res = $mysqli->query($query);
$db_queries['q3'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_1 WHERE field_30 <> ''";
$res = $mysqli->query($query);
$db_queries['q4'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_2 WHERE field_60 <> ''";
$res = $mysqli->query($query);
$db_queries['q5'] = $res->num_rows;
foreach($db_queries as $k => $v){
echo $k, " contains ", $v, " rows.";
}
I have a database with two tables, Users and Posts. I'd like to write a php script to list, in order, the top 20 usernames with the number of posts they've made. The username field is cUsername in the Users table. The Users table intUserID field and the Posts table's intPosterID field correspond to eachother.
Any help would be much appreciated. Thanks!
Use GROUP BY and count. This will get you a list of user IDs to their counts:
SELECT intPosterId, COUNT(*)
FROM Posts
GROUP BY intPosterId
ORDER BY COUNT(*) DESC
LIMIT 20
You can use the result in a subquery:
SELECT u.cUsername, pcnt.postCount
FROM Users AS u
INNER JOIN (
SELECT intPosterId, COUNT(*) as postCount
FROM Posts
GROUP BY intPosterId
ORDER BY COUNT(*) DESC
LIMIT 20
) AS pcnt
ON u.intUserId = pcnt.intPosterId
To use it in a PHP script:
<?php
$sql = '
SELECT u.cUsername, pcnt.postCount
FROM Users AS u
INNER JOIN (
SELECT intPosterId, COUNT(*) as postCount
FROM Posts
GROUP BY intPosterId
ORDER BY COUNT(*) DESC
LIMIT 20
) AS pcnt
ON u.intUserId = pcnt.intPosterId
';
$pdo = new PDO(
'mysql:host=your_host;dbname=your_db',
'username',
'password',
array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)
);
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "{$row['cUsername']}: {$row['postCount']} <br />\n";
}
I'm wanting to find the ranking / number of a row. I'm not sure if I'm explaining this well so I'll try.
I have the query
$sql = 'SELECT SUM(amount) AS total FROM sales ORDER BY total DESC';
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
// go through and print each row biggest 'total' first
echo $row['total'] . '<br />';
}
Now I want to go through and give each a ranking based on the biggest 'total' being number '1'.
So I can do that with php by doing some counting:
$sql = 'SELECT SUM(amount) AS total FROM sales ORDER BY total DESC';
$res = mysql_query($sql);
$rank = 1;
while($row = mysql_fetch_array($res)) {
// go through and print each row biggest 'total' first
echo 'rank: ' . $rank . ', ' . $row['total'] . '<br />';
$rank = $rank + 1;
}
This is good and working. But what I'm wanting to do, is be able to determine the ranking of a row without php so I can do an sql query based on say an affiliate ID from the sales table.
So for example I have 100 rows of sales data with an affiliate ID linked to each row, how would I go about simply getting the ranking based on the affiliate with the biggest total?
You can use a recursive variable to do this, like so:
select
#rownum:=#rownum+1 as rank,
sum(amount) as total
from
sales,
(select #rownum:=0) a
order by total desc
To grab the ranking of a given affiliate, you would have to do:
select
a.*,
t.rank,
t.total
from
affiliates a
inner join (
select
#rownum:=#rownum+1 as rank,
affiliate_id,
sum(amount) as total
from
sales,
(select #rownum:=0) r
group by affiliate_id
order by total desc) t on
a.affiliate_id = t.affiliate_id
where
a.affiliate_id = 342
Now that's (relatively) slow, because you have to do a table scan each time.
If you weren't using MySQL, I'd suggest a subquery, but by and large MySQL optimizes subqueries horrendously. It seems like it'd be fairly slow to me, but I haven't benchmarked it. At any rate, you can do this:
select
a.*,
(select
count(*)+1
from
(select affiliate_id from sales
group by affiliate_id having sum(amount) >
(select sum(amount) from sales where affiliate_id = a.affiliate_id)))
as rank
from
affiliates a
where
a.affiliate_id = 342
Other implementations of DB has row_number and row_number_count. If you are using MySQL, I suggest you take a look at this walk-around