Retrieving count of column mySQL PHP - php

Hey guys I have a mysql table called interests with 4 columns. interestID, name, categoryID interest_desc and date. the categoryID column is linked to a seperate table. How would I use a mysql query that checked how many interests are in a certain category?
Im guessing i use some sort of count() query?
Thanks guys
Update -
$count_query_v1 = "SELECT categoryID, COUNT(*) AS total FROM interests GROUP by categoryID; "; $answer = mysql_query($count_query_v1) or die(mysql_error()); echo $answer;
Getting closer but still not perfect, i want to echo out the categoryID with the most interestID's

select category_name, count(*) as total
from interests i left join category c on c.category_id = i.category_id
group by i.category_id;

count + group by,
assuming interestID is the unique primary key,
and each interest is tied to single category (as what you have shown)
select categoryID, count(*) as total
from interests
group by categoryID;
// the above example is a simple group by ID without using inner join
output :-
categoryID, total

SELECT COUNT(interestID) FROM interests WHERE categoryID = 'yourvalue';
SELECT COUNT(interestID), categoryID FROM interests GROUP BY categoryID;

Since you are using the insert query each query will insert one record, so just count the number of insert queries you run by using a counter varialble.

Related

joining 2 tables in msqli

table posts
table users
how would i count posts for specific user logged in. for example when user with id 3 is logged in it should show me 4 posts
I already did it for total posts count:
<?php
$post_query1 = "SELECT count(*) AS total FROM posts ";
$post_result1 = mysqli_query($db, $post_query1);
$post1 = mysqli_fetch_array($post_result1);
?>
Try below example :
select count(*) as total from user as u inner join post as p on p.id_user = u.id_user AND u.id_user = 3
If you want to get only the posts count for the particular user, say user with id = 3, your query should be this:
$query = "SELECT count(*) AS total FROM posts WHERE id_users = 3";
But if you want to get both the posts count as well as the user information and other post information, you will have to run a join query on both the users and posts table. Your query would now become:
$query = "SELECT u.*, p.*, count(p.id_posts) FROM users AS u JOIN posts AS p ON u.id_users = p.id_users WHERE p.id_users = 3";
Some Useful Notes
p.* - * is a wildcard character that means get all the columns in the posts table
u.* - * is a wildcard that means get all the columns in the users table
posts as p - AS is for aliasing. So, we are giving posts table a temporary name.
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
Note: It is necessary that you have to join two/more tables only with the help of foreign key. Without the foreign key is is meaningless to join two or more tables
Reference 1: https://www.w3schools.com/sql/sql_join.asp
Reference 2: https://www.tutorialspoint.com/mysql/mysql-using-joins.htm
As per the Question what you have asked to join the tables
Query:
SELECT * FROM TABLE 1 JOIN TABLE 2 ON TABLE1.id = TABLE2.id WHERE TABLE2.ID=3
Kindly replace TABLE1 & TABLE2 with the Tables that are to be joined and the id with the foreign key what you have specified in the Table.
Hope so this might be helpful for you to write your own code in future. Happy Coding :)
You have only to use a simple join.
SELECT count(*)
FROM USER u,
post p
WHERE p.id_user = u.id_user
AND u.id_user = 3

How can I get values from both my tables?

Let's say I have to MySQL tables like this.
TBL_1
ID - NAME - DESCRIPTION
1 foo very nice
TBL_2
ID - PRICE - CATEGORY - QUANTITY
1 10 a 5
If I was to set up a PDO instance like so....
<?php
$handler = new PDO("XXXX;XXXXX","XXX","XXX");
$query = $handler->query('SELECT * FROM TBL_1');
while($r = $query->fetch(PDO::FETCH_OBJ)) {
echo $r->id;
echo $r->name;
echo $r->description;
//echo $r->price;
//echo $r->category;
//echo $r->quantity;
}
How can I access price, category, quantity where the ID's, in both tables are equal to each other?
So for example, it would come out like this.
1 foo very nice 10 a 5
You might use JOIN:
SELECT name, description, price, category, quantity
FROM TBL_1
JOIN TBL_2
USING (id)
Hope it helps.
Assuming the ID columns contain the same values (i.e. a foreign key constrait exists because the values represent the same entity), you'll want to execute a query with an INNER JOIN on that column. By default with an INNER JOIN, if the requested value (for instance a WHERE 'ID' = 3 clause) does not exist in either table, no results will be returned. Try the following:
SELECT *
FROM `TBL_1` AS `t1`
INNER JOIN `TBL_2` AS `t2` ON `t1`.`ID` = `t2`.`ID`;
You just do an INNER JOIN on the table where on each table the id is equal.
SELECT * FROM TBL_1 INNER JOIN TBL_2 ON TBL_1.ID=TBL_2.ID;

SQL Query count issue

I've two tables artist and members. artist has zero to many relation with members. (one artist may have 0 or many members)
So, I want to get all the fields of artist with the total number of members, that he has.
I write a query, but, it returns 0 count if there is no artist in the table (artist table is empty). I am using this query
SELECT a.artistname, count(m.id) as total
FROM artist a, members m
WHERE m.artist_id = a.id
It should simply not return anything if there is no artist.
Thank you!
I'm assuming that your field name is artistname in the artist table,
The query should go like this:
SELECT artist.artistname, COUNT(members.id) AS total_members
FROM artist
LEFT JOIN members ON members.artis_id = artist.id
GROUP BY artist.artistname
Use join, and to use sql count you need to use group by.
Perhaps the artistname is empty. Did you check the data? You could try the following modified query:
SELECT a.artistname, count(m.id) as total
FROM artist a, members m
WHERE m.artist_id = a.id
AND a.artistname is NOT NULL
AND a.artistname <> '';
This should work:
SELECT a.artistname,
count(m.id)
FROM artist a
LEFT JOIN members m
ON m.artist_id = a.id;

MySQL select songs from ids in other table

How can i select all the id's from a table, and let select all the songs with the id;s from the other table?
$query = "SELECT * FROM songs WHERE id = (SELECT songid FROM top10 order by id)'";
You can use IN for that (or EXISTS):
select *
from songs
where id in (
select songid
from top10 )
Based on your comments, you might actually be looking to use JOIN (just realize if there are duplicate records in the top10 table, this could return duplicate results):
select s.*
from songs s
join top10 t on s.id = t.songid
order by t.id
SQL Fiddle Demo

sorting mysql table by data within a different table

I have to mysql tables and trying to display results of table1 but sorting by table2. For table2 I'm counting all the occurrences of a duplicate id and then display that result descending. Below is as far as I could get and wondering if this can even be done is a single query.
$query = "
SELECT DISTINCT registration.*
FROM registration
INNER JOIN downloads
ON registration.id = downloads.id
GROUP BY downloads.COUNT(id)
ORDER BY downloads.COUNT(id) DESC,
downloads.COUNT(id) DESC
";
I think you want something along these lines:
SELECT Registration.id, Registration.name, Registration.email,
Downloads.document
FROM Registration
JOIN Downloads
ON Downloads.id = Registration.id
JOIN (SELECT id, COUNT(*) as count
FROM Downloads
GROUP BY id) Download_Count
ON Download_Count.id = Registration.id
ORDER BY Download_Count.count DESC
(untested, as it would be nice for the OP to supply sample data and table layouts in the question)

Categories