I searched everywhere, any online tutorials, but they are all dated and badly composed. I was looking for a way to count visits to a particular post, but only unique visits via ip address.
I am not unfortunately an expert, I have tried some ways but without success.
So I asked how to do it?
I create a table in the database called "views_post" with the various fields: ip_address, id_post, views_count.
I should create insert or update queries that add +1 to the "views_count" field of the specified post ID. Let's verify if an IP address is already present, so it will not insert / update the table.
I do not know how to run it all, I did some tests but only put the first value but it does not update anything at the next visits.
Any advice is welcome, thank you.
You don't need an update .. you could simply add each single access to a table visit
id_address,
id_post,
date_visit
and if you need distinct count for post you could use count(distinct ip_address) total or by date eg:
select id_post, count(distinct ip_address)
froom my_table_visist
group by id_post
or
select date_visit, count(distinct ip_address)
froom my_table_visist
group by date_visit
Related
I have a script where members login and read posts by catagory that I have in
Table called posts, then they click a button and an entry is inserted into Table
called postsread collecting the postname, their memberid, and the date showing
that it had been read by them. What I am looking for is a query that will
display to them only the posts that they have not already read.
**Tables** **Fields**
posts id, name, date, from, topic, info, cat
postsread id, postname, memberid, date
users id, memberid, pass, fname, lname, email
Sessions is already holding their $_SESSION['memberid'], but am unsure of how
to query between the two tables to get what I'm looking for.
It would be like: Show all posts in Posts except those in Postsread with the members
memberid next to the corresponding postname.
I am using php version 5.3, and mysql 5.0.96.
I am using the following to display posts from database:
$sql=mysql_query("SELECT * FROM posts WHERE cat='1' ORDER BY date DESC");
But this does not differentiate between if the member has clicked stating they have
seen them yet or not.
I have looked many places and see examples that are close but just cant get any to
fit what I am needing. I don't fully understand how to write this. I have tried many
with no success. If you need extra description please ask. Thank you for your time.
You need to construct a JOIN condition against the posts_read table, or use an IN clause to exclude them. You should be very careful with your indexes as these sorts of queries can get extremely slow and database intensive with non-trivial amounts of data.
SELECT * FROM posts WHERE cat=:cat AND name NOT IN (SELECT postname FROM postsread WHERE memberid=:memberid)
Here :cat and :memberid are placeholders for the appropriate values. Using PDO you can bind directly to those using the execute function.
As a note, joining on strings is a lot slower than joining on id type values. You might want to make your postsread table reference id from posts instead.
sql=mysql_query("SELECT * FROM posts WHERE cat='1' AND name NOT IN (SELECT postname FROM postsread WHERE memberid='$memberid') ORDER BY date DESC") or die (mysql_error());
Tadman was right on with his answer; I had something in the code that was not supposed to be there, but could not find it. Eventually I just rewrote the thing and it worked. The above worked for me, and I thank you Tadman for your help.
I need to build a SQL query which is beyond my programming abilities.
Okay, here is my request:
Let's say I have a table, with: id, user_id, email and amount columns. This query, should SELECT user_id only once! If I have matched user_id once, the query shall continue, but if the same user_id is matched again in another row, we should skip it.
Here comes the main problem...
Imagining that we grabbed user_id once, and skipped all same other rows with that user_id, now I need to sum all the contents from the amount column for the same user_id.
I think I complicated this a bit, I'll try illustrating my issue:
If this problem is not solvable via SQL only, then a PHP answer would work too.
I'm trying to create a list of users (no duplicate users) and add the amount they paid.
According to the image, user_id 56 paid 90.00 (12 + 45 + 33)
Can someone tell me a way how to achieve this?
Assuming the name of the table is users
SELECT user_id, email, SUM(amount) FROM users GROUP BY user_id
I am working on a PM system where I'd like to have the previous sent PMs for one conversation, listed above the last received PM. But my question is: how do I go about setting up such a table in a database? I toyed for a while about using an id for each specific conversation, but what would the source for that id be? I can't use auto increment (it seems), because I'm using it for the primary "id" column.
Or maybe there's a completely different way I can experiment with the already available columns (id, from, to, subject, message, sent, read, deleted); but how? Please help a lost man out.
You could add a origin_id column to your table that contains the id of the root/original message, or NULL if it's a new discussion (root).
Then you can get the root messages by filtering those than have origin_id = NULL and then group by origin_id to get the message thread.
Okay, so I have got it partly solved...
I used another table containing the one column which holds the subject of the PM. I also have a new column in the regular "pms" table that holds the same ID to be able to join the tables together.
However, when I select all the PMs to show them in the inbox, I have not found a way to group the conversations in order by if they're read or not. I'm currently using this SQL query:
SELECT *
FROM `pms`
JOIN `pm_conversations` ON (pms.ConvID = pm_conversations.ID)
WHERE pms.To='username'
GROUP BY pm_conversations.ID
ORDER BY pms.ID
I came up with this:
SELECT MAX(pms.ID) as pmIDS,
pms.*,
pm_conversations.*
FROM `pms`
JOIN `pm_conversations` ON (pms.ConvID = pm_conversations.ID)
WHERE `To`='".$UserActive."'
GROUP BY pm_conversations.ID
ORDER BY pmIDS DESC
Hi I have a question regarding a method to prevent second voting in a website where people can vote up or vote down a comment. Like Stackoverflow!! :)
The question is how I can keep track of whether a comment has already been voted by a logged in user. Lets assume this is for a online ecommerce store with reviews on products. Here's what I think:
All reviews data is stored in a table called 'reviews'
Field names: review_id, product_id, user_id, title, description, time_created, up_votes, down_votes, up_voters, down_voters
up_votes and down_votes contain the number of votes voting it up or down
up_voters and down_voters contain the user_id of the people who voted up or down in the format 1101.1102.1103 for users with user_id 1101, 1102 and 1103.
When a person clicks on the up vote button, the system will check if the current user's user_id matches anyone in up_voters. An explode function will turn 1101.1102.1103 into an array and in_array() will be used to check if the user has already voted.
Is there a better way to go about doing this?
That table will give you nightmares down the line. Just think of a hit product with 1K up_voters, your field will be 10K*4 characters long! Not just that, it will hinder your ability to make reports and study the data etc.
I don't know much about your needs but from what I can see in your table, I'd suggest the below.
review
user_id, product_id, type_of_vote, title, description, time_created
Use above table and use user_id, product_id as the key
OR
review
review_id, product_id, title, description, time_created
vote
review_id, user_id, type_of_vote
There are n number of ways to design tables for this. If it is an Operational Data Store(ODS) then you might want to normalize your design. If it's a warehouse then you may consider first table I mentioned above.
Sorry for the crappy title - didn't know how else to say it.
I have a commenting database where anyone can report a comment for abuse (anyone as in not just logged in members). This obviously means a person could report a comment multiple times. Where I need help is building a query that shows the number of times a comment has been reported but by unique email address AND ip address.
I'm not sure if that is clear or not -- if joe#joe.com of IP address 1.2.3.4 reports a message more than once, it will only count as one report. If there is an additional record of joe#joe.com with an IP of 4.5.6.7 reporting the same comment, it would count as two reports since the IPs are different.
The fields, all in one table:
id
comment_id
note
email
ip_address
I hope that all makes sense and any help would be GREATLY appreciated!
SELECT email, ip_address, COUNT(DISTINCT note)
FROM log
GROUP BY
email, ip_address
Update:
As per your comment, I believe you want this:
SELECT comment_id, COUNT(DISTINCT email, ip_address)
FROM log
GROUP BY
comment_id
SELECT
comment_id,
COUNT(DISTINCT CONCAT(email,'|',ip_address))
FROM log
GROUP BY comment_id
As OMG Ponies said, adding a unique in email+ip_address would help, possibly with this to prevent lost notes:
INSERT INTO log (...) VALUES (...) ON DUPLICATE KEY note = CONCAT(note,' ',VALUES(note));