PHP Mysql count distinct reasons given where reason value in another table - php

I want to create a statistics page where i can count the number of reasons given in a table ...The reasons are created dynamically and stored in another table.
Table1 contains all the reasons which populate a drop down box...
Table2 contains the entries given by the user one of the columns is the reason value.
I would like to count the amount of times each reason appears .
Thankyou for your time and help

Looks like simply group statement:
select
reason.name reason,
count(*) count
from entry, reason
where
entry.reason = reason.id and
entry.time > now() - interval 7 day -- you probably need some filter
group by reason.id

Related

Select from database with count and count based conditions?

I have a table with data relating to a user, and two important columns:
refer_count, which is updated when a new entry is made in the table with the referred_by column set to that users user_id, and referred_by which is the user_id of the of the user that referred them.
I want to select the users from the table that have the highest number of referrals after a certain date.
For example:
If there are 3 users, one of which referred the other 2 (lets say users 2 and 3), however user 2 was referred on the 2/12/14, whereas user 3 was referred on the 3/1/15.
If the cutoff is 1/12/14, then user 1 is returned with refer_count set to 2, but if the cutoff is after 2/12/14, then user 1 is returned with refer_count set to 1.
I've been thinking of how to do this, but I can't think of a way that would work. Is there a way?
This is via MySQL.
EDIT: I think I may need to provide for information.
The date registered (register_date) is used as the refer date. I need the refer_count to be updated with the number of users referred after the cutoff, however I need to get the actual user. This is for a 'top referrers' table. I can't figure out why I'm having so much trouble thinking of a way to do this.
SELECT user_id FROM usertable WHERE (referal_date BETWEEN '2014-12-2' AND CURDATE())ORDER BY refer_count DESC;
That's the rough idea.
You should look into normalizing your tables if you're keeping that all in the same table, though. It'd be better to keep referals in a seperate table.
Get the row with the maximum in refer_count with a Date condition for your referal_date such that it's after the certainDate:
SELECT user_id FROM table WHERE refer_count = (SELECT MAX(refer_count) FROM table) AND referal_date>certainDate;
Note that WHERE is before SELECT so it will not get the highest count first, but will filter with the date condition then get the highest count.
Edit: Updated query based on edited question.

Repeated Insert copies on ID

We have records with a count field on an unique id.
The columns are:
mainId = unique
mainIdCount = 1320 (this 'views' field gets a + 1 when the page is visited)
How can you insert all these mainIdCount's as seperate records in another table IN ANOTHER DBASE in one query?
Yes, I do mean 1320 times an insert with the same mainId! :-)
We actually have records that go over 10,000 times an id. It just has to be like this.
This is a weird one, but we do need the copies of all these (just) counts like this.
The most straightforward way to this is with a JOIN operation between your table, and another row source that provides a set of integers. We'd match each row from our original table to as many rows from the set of integer as needed to satisfy the desired result.
As a brief example of the pattern:
INSERT INTO newtable (mainId,n)
SELECT t.mainId
, r.n
FROM mytable t
JOIN ( SELECT 1 AS n
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
) r
WHERE r.n <= t.mainIdCount
If mytable contains row mainId=5 mainIdCount=4, we'd get back rows (5,1),(5,2),(5,3),(5,4)
Obviously, the rowsource r needs to be of sufficient size. The inline view I've demonstrated here would return a maximum of five rows. For larger sets, it would be beneficial to use a table rather than an inline view.
This leads to the followup question, "How do I generate a set of integers in MySQL",
e.g. Generating a range of numbers in MySQL
And getting that done is a bit tedious. We're looking forward to an eventual feature in MySQL that will make it much easier to return a bounded set of integer values; until then, having a pre-populated table is the most efficient approach.

How to find the next available integer in MySQL table using PHP

I know auto_increment is the way to go but I can not use auto_increment feature since the column in my table might repeat, its not unique. When I insert a new row to a table I need a way to find the next available spot to insert it.
For example table structure:
Primary Key = (ID, UserID)
ID UserID
3 6
3 1
1 3
Now when i do insert query i want to isert it at ID = 2 and not 4. With auto_increment it gives me 4
Is there a solution without using the loop in PHP? So far what i have is I fetch all rows into array and then find the next available digit in ID. Is it possible to do this without fetching all rows in PHP and just doing it on MySQL query ?
SELECT t1.id+1 AS MISSING_ID
FROM the_table AS t1
LEFT JOIN the_table AS t2 ON t1.id+1 = t2.id
WHERE t2.id IS NULL
ORDER BY t1.id LIMIT 1;
I made a fiddle: http://sqlfiddle.com/#!2/4d14d/2
No, it is not possible without processing the data. The preferred method to correct this issue is to adjust your table structure to support a unique, auto-incrementable field. Failing that, you will have to process the data (either in PHP or via an SQL statement) to find an open slot.
This should do the trick:
SELECT
min_table.ID+1 AS start,
MIN(max_table.ID) - 1 AS end
FROM
your_table AS min_table,
your_table AS max_table
WHERE
min_table.ID < max_table.ID
GROUP BY
min_table.ID
HAVING
start < MIN(max_table.ID)
The left hand column will return the first available spot in the sequence gap, and the second is the highest number in that particular gap.
Source: http://www.codediesel.com/mysql/sequence-gaps-in-mysql/
My workaround for not loaded project:
Suppose, you have questionset with question_id 's which belong to certain topic_id.
Suppose, user navigates and clicks "<Prev" "Next>" buttons to navigate questions.
You have only current id. Catching the direction of navigation, topic_id, question_id you can do a loop
do {
// query base, doing question_id++ or question_id-- depending on needed direction until you find next id within topic_id
} while( id!=null ) `
using incrementation or decrementation depending on direction of your move

Alternative solution for PHP/SQL max array size problem

I have a table bundled among 100 databases in MYSQL (i.e. 1st x rows of the table in database_1, 2nd x rows of the table in database_2, ... , last x rows of the table in database_100)
Each table has a row whenever a user visits a friend for a game.
The columns are iuin, logtime, beuin.
iuin is the user id of the visitor.
beuin is the user id of the friend who was visited.
logtime is when the visit was made.
I would like to find the # of distinct friends who were visited during a week.
There is roughly 300k distinct users who are visited per day.
However, when I extended my code to calculate for a week, I ran out of memory.
My code basically does an SQL query using SELECT DISTINCT beuin for a selected week for the table in each database. I store all the beuin in an array if it's not already stored (so I count distinct friends who were visited), and return the size of the array at the end.
FYI, I can't edit the database around such as joining all the tables in different databases into one table.
Is there any alternative ways i can do this?
Thanks
It's hard to say anything about your without the one. But I think you can solve this problem using mysql. My quick solution:
Create table - CREATE table if not exist users_ids(user_id INT NOT NULL DEAULT 0 PRIMARY KEY(UNIQUE)); in the first db
Truncate users_ids
Run 100 queries like INSERT IGNORE INTO db1.users_ids select distinct user_id from db1.table1;
Select count(*) from users_ids;

Postgresql Query

I have working in database Postgresql for the first time. I need your help to finding out a solution. One table contains 15 rows With a regn_srno as P.K., another table has the same regn_srno as F.K. I want to count the total number of rows which has the same regn_srno. But My problem is the second table has contain 2 or 3 fields with the same regn_srno. So when i use count in the query it shows 12 (including the same regn_srno), but the original number is 10. Due to the same regn_srno repeat in the second table i got the answer as 12.
When we group by regn_srno we get the result as 1,1,1,1,2,1,2,1,1,1. So i need the query to get the count as 10.Please help me. Please send me the answer through my mail id.
For what I could figure out without tables schema, I think you want
SELECT count(DISTINCT regn_smo) FROM t1 JOIN t2 USING (regn_smo);
You could simply do:
SELECT count(DISTINCT regn_smo) FROM t2

Categories