MySQL -- Keeping Track of User's Posts [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on a project where users will be able to make posts that will be stored in a MySQL database. I need an efficient way to list the posts of one specific user and a way to list the posts of all users. I thought about using one table for all posts, but I am afraid that that would be slow. I could also have a different table for the posts of each user, but I would then not be able to search for posts by all users. Would it be efficient to combine the two methods above, or is there a better way?

For each post a user does, make note of his ID (a unique identifier) in the row, so that in the future you can select all the posts that a certain ID has done.
Example (pseudo sql structure):
tblPosts {
PostID int(11),
AuthorID int(11),
PostText text
}
tblAuthros {
AuthorID int(11),
AuthorName text
}
This is very efficient and fast

Create a table like so:
--posts--
id
post_body
user_id
date_posted
Where user_id is the unique ID of the user that created the post. Then select said posts like so:
SELECT id, post_body, user_id, date_posted FROM posts WHERE user_id = 8723

Related

Implementing voting or "likes" using MySQL [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am creating a system where users (who are identified by a user id number) will be allowed to vote on posts (think Reddit, StackOverflow, etc).
Users can vote a post up or not vote at all on it.
The number of votes on a given post can easily be stored within the table containing the posts.
Keeping track of who has voted, however, is a different task entirely that I'm not sure how to approach.
I was thinking I could have a table that would have two columns: user id and post id.
When they vote on a post, I add their user id and post id to that table. If they unvote, I remove that entry from the table.
EG:
User ID | Post ID
1 | 3949
1 | 4093
2 | 3949
etc...
Is this a reasonable solution?
Yes this is reasonably simple and easy solution to the problem. You can do the same for your comments(if you like to). In your MAIN_POST table assign a post_id and use this same post_id in other tables (comments(post_id, user_id, post_comment, comment_time) and votes(post_id, user_id, vote_status(you can use 1 for vote up and 0 for vote down))). It will complicate your sql queries, to retrieve data, a little but you can do it. And on android side there are alot of tricks to handle and furnish this data in application and you can make this vote(like) and comments idea just like facebook (YOU for your comments and likes and NAMES for others).
I wouldn't remove rows from the table. I understand why you would want to do that, but why lose the information? Instead, keep a +1/-1 value for each entry and then sum up the values for a post:
select sum(vote)
from uservotes
where postid = 1234;
And, I agree with Rick that you should also include the creation date/time.
Using an 'in between' or 'joining' table is a perfectly acceptable solution in this case. If relevant you could even add a timestamp to the relation and show to the user when a user has upvoted something.
Also it is important to take care of proper Indexes and Keys to have your table structure also perform properly once the dataset grows.

Get a data from a different table using id [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a table in my database called users and in this table there is a field called rank. This field holds the id of the users rank.
I also have a table called 'ranks' in my database, and this table has id, rank_id and rank_name, the id being the primary auto increment field, the rank_id hold the rank id and this is what is being used in my users table.
All is good. I need to display the users rank_name, not the rank_id when I output what rank they are. Im not a pro at mysql, i am still learning.
How do i get the users rank, match it up to the rank_id in the table ranks and then output the rank_name from the corresponding rank_id?
Please answer this using mysql_*. I know its life is ending soon but this is what im practicing with at the moment, will help me learn how to convert to mysqli_* or PDO.
You will need to use a query like this
SELECT u.name, r.rank_name
FROM users AS u
LEFT JOIN ranks AS r ON u.rank = r.rank_id
I would suggest you read about the different joins. Also remember you can join multiple tables in this fashion.

Inserting content into MYSQL table columns with certain search criteria [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am pretty new to MYSQL tables, pretty limited experience.
I have a wordpress table (using PHPmyadmin) and basically, I want to add template content into all posts that have the below search criteria.
I honestly have no idea, I just need the bit where I can insert text (template text) all at once, not individually to the posts with the below criteria.
SELECT *
FROM `wp_posts`
WHERE `post_title` LIKE 'best hotel deals in%'
AND `post_status` LIKE 'draft'
Hope this makes sense!
OK, the post_content fields are blank at the moment and I want to fill it with basic text for example:
"the fox jumps over the fence"
you need to learn SQL, simple solution for you is:
UPDATE `wp_posts` set `colname`=CONCAT(`colname`, 'additional content')
WHERE `post_title` LIKE 'best hotel deals in%'
AND `post_status` LIKE 'draft'
if you want to update all posts - just remove where... condition
If you just want to add text to the beginning or end of the post content, you can do something like this:
update wp_posts
set post_content = concat(post_content, 'extra text you want to add')
where
post_title like 'best hotel deals in%' and
post_status = 'draft'
Be careful, though, particularly if you're not an experienced MySQL or WordPress user. Messing around directly in the database tables can really screw up your WordPress installation if you're not careful.

MySQL - Return all columns where bit is equal to 1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working with a table that contains 50 columns (with bit values), for each 50 U.S. state. I'm trying to find an SQL statement that will return all columns containing a 1. I've looked around for ways of doing this with no luck.
Any help would be much appreciated
If this is a new development, and not for existing tables I would suggest the following. (Otherwise I'm stumped.) It sounds like you have implemented a many-to-many. I would suggest that you implement a table for States as suggested by #juergen d (albeit without the bit field); implement your current entity as-is but without the columns for states. And then implement a third table that has two columns, one for a state's ID and another for the ID of your entity.
Then instead of setting bit fields on your entity's table, you would create entries in this third table.
You can then perform joins on tables to obtain the states set on a certain entity.
For more info see http://en.wikipedia.org/wiki/Many-to-many_(data_model) and http://www.lornajane.net/posts/2011/inner-vs-outer-joins-on-a-many-to-many-relationship
You should better change your table design. Better use something like this
States table
-------------------
id int
name varchar(100)
bitcol bit(1)
Then you could select that states like this
select name from States
where bitcol = 1

Draw The Highest Value Out Of A Mysql Database Column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a database "videos". Which has a table for each video i.e "ID" "Name" "Link" "Count".
i want to be able to take the highest value of any of the tables from the column "Count" and then use which ever has the highest values "Link" Column.
So the best way i can explain is i'm trying to make a "Most Viewed" part of my site so which ever has the highest hit count gets displayed via $row['link'].
Hope this made sense didn't much to me!
Thanks all in advance...
SELECT `id`,`name`,`link`,`count` from my_table order by `count` desc limit 5;
This will give you 5 most viewed links.
Tip : You should not use words like count as column names as they are MySQL keywords.
SELECT `link` FROM `someTable` ORDER BY `Count` DESC LIMIT 1
This will give you the link of the row with the highest count.

Categories