Same id's but only fetch 1 result - php

I'm about to make a message system. Instead of making 2 tables like conversations and messages, I would just like to have messages. There would then be a lot of rows with the same user id's like this:
id to_id from_id message
1 1 2 text...
2 1 3 text...
3 2 1 text...
4 1 2 text...
5 1 2 text...
6 1 2 text...
The thing is, that I want to get one of each conversation. If id 1 writes to id 2, then thats 1 conversation, but if id 2 writes a reply to id 1, then it would STILL be only 1 conversation.
In the above example, there is only 2 conversations (id 1 to id 3) and (id 1 to id 2 and reverse).
How can i Achieve this. My idea is that there must be something simpler than DISTINCT.

I might go for a related table but to keep it simple, just add the column conv_id.
When a message is created, create a new conv_id and add it to the table row with the other data. When a message is replied to just use that conv_id for the new row.
When you want to retrieve conversation(s) either select WHERE conv_id=x and/or use a GROUP BY conv_id.

Why don`t you use group by to join conversations :
select * from table where to_id = 1 group by from_id

Related

Pull unique data from a database - MySQL

I'm looking to query a table that contains messages, organised with a sender and receiver. I would like to create a query to only return each combination of sender and receiver once. I currently have data stored in a database like so:
sender
receiver
1
2
2
1
3
4
4
3
The output I am looking for is as so:
1 | 2
3 | 4
Is this possible? If so, any hints in the right direction would be much appreciated.
One option uses least() and greatest(), and distinct:
select distinct least(sender, receiver) as user1, greatest(sender, receiver) as user2
from mytable

Delete Particular Id From Column using mysql

Two tables named are image and product the structure of the tables are as below
Table : Image
image_id image_path image
1 ./images/ aaa
2 ./images/ bbb
3 ./images/ ccc
Table : Product
product_id product_gallery
1 1,2
2 3,1
3 1,2,3
Now I want to remove the image from image table image_id = 3
While I'm deleting image id 3 from image table, at the same time product table product gallery column containing the id 3 will be deleted.
For example
Expected output :
product_id product_gallery
1 1,2
2 1
3 1,2
I don't have any idea for this but I can delete the images as of now, still I want to delete image id from the product table.
It's better to have a 3-rd table , some thing like :
Table : product-gallery
product_id image_id
1 1
1 2
2 1
3 1
3 2
In this way, working with database would be as easy as abc :)
But if you want to go your way, you have to deal with some sql or php processing functions.
i think it is better if you change the way your product table was constructed.. it would give you a hard time retrieving and manipulating it.. try using this format:
id product_id product_gallery
1 1 1
2 1 2
3 2 1
etc etc...

Count how many times a value appears in sql query using php

I have created a database and website that will be used by football managers to select their team etc. Once a match has been completed events will be stored in the match_players table. Such events are Goal, Yellow Card, Red Card etc. I have no problem getting this information into php from SQL db.
I need to add up how many times a Goal appears (a '1' is placed in the SQL table) and for what team so that a final score can be displayed. So, for example, if Team A has 1 goal and Team B has 2 then I need to display that. I am trying to count the amount of times that a Goal is registered in the table. Any help will be greatly appreciated.
You can use MYSQL SUM
select SUM(Goal) from match_players where Team="A"
Or you can get the same for all teams by
select Team,SUM(Goal) from match_players group by Team
Why don't you demand this sum to SQL directly?
SELECT SUM(goals)
FROM match_table
WHERE team = 'Barcellona'
This should be much faster also than elaborate all data at "php-level"
If you want this detail for all teams
SELECT team,SUM(goals)
FROM match_table
GROUP BY team
Well if you store a 1 each time a goal is scored, your table looks like this:
TeamID goal
1 1
2 1
1 1
3 1
2 1
2 1
1 1
So you just want a count of how many times a team appears in that table:
select TeamID, count(*) from table group by TeamID
Will give you
TeamID | count(*)
1 | 3
2 | 3
3 | 1

I need a table for user posts (allowed to see only friend, specific persons etc.)

When user post a message, he can select: allowed to see only friend, specific persons etc..
mysql table:
Post
post_id post_nr user_id privacy option
1 1 2 allowed for friend id1
2 1 2 allowed for friend id2
or
1 1 2 allowed for friends id1,allowed for friend id2 .?
How i can register in mysql multiple options(privacy) for one post?
The design of table is ok?
You can solve it by connecting a privacy_option table to your post table.
post
post_id post_nr user_id
1 1 2
privacy_option
post_id friend_id
1 3
1 4
This way several friends can be listed per post and you can get a list of friend ids with a simple JOIN. Make sure to mark privacy_option.post_id as a foreign key pointing to post.post_id.
It would be better to have a seperate table for the privacy option. And I'm not sure what you mean by "post_nr" .
Table : POST
post_id | post_nr | user_id
Table:PRIVACY
post_id | privacy_option

Retrieving "likes" tied to users from a database

I'm new to database structure. I'm trying to create an app that allows users to like certain entries, but I want to be able to tie likes to users so that I can change the visuals before/after the like action.
I think from research that I should have an 'entries' and 'users' table and then have a 'likes' table that ties the two to each other.
The only thing I'm unsure of is, when getting and displaying the contents... how would I write the queries? If I query for all the entries I need, do I then go back and individually query each to see if it has a like tied to it for the current user? That seems like it might be a costly operation. Is there a more efficient way?
Hope that makes sense,
Thanks.
I think you have the right database design in mind. As far as queries are concerned, assume tables as such:
Users
ID | Name
1 | Bob
2 | Sally
Entries
ID | Name
1 | Red
2 | Blue
3 | Yellow
Likes
UserID | EntryID
1 | 1
1 | 2
2 | 2
2 | 3
So we can say Bob likes Red and Blue while Sally likes Blue and Yellow. So a query to retrieve all entries, plus an indicator of what Bob likes would be:
SELECT
e.ID,
e.Name,
l.UserID
FROM Entries e LEFT JOIN Likes l ON l.EntryID = e.ID
WHERE l.UserID = 1 -- Bob's User ID
ORDER BY e.Name
This would return
ID | Name | UserID
2 | Blue | 1
1 | Red | NULL
3 | Yellow | 1
The UserID column indicates if Bob likes the entry or not - a NULL is No and a value is Yes.
Assuming you have a table Entries with a column entity_id (and whatever else you store about the entity) and a second table UserLikes that contains the columns user_id and entity_id, you would do the following:
SELECT Entries.col1, Entries.col1 . . ., UserLikes.user_id
FROM Entries LEFT OUTER JOIN UserLikes ON
Entries.entity_id = UserLikes.entity_id
WHERE UserLikes.user_id = :user_id
AND Entity.col_whatever = :whatever
In this example, Entries.col1, Entries.col2 . . . is the list of columns you want to get back about the Entries. The :user_id is a parameter that contains the id of the user you're currently trying to display Entries for. And the last line is standing in for whatever limitations you want to put on the Entries are returned.
This query will give you a row for each Entry you searched for. You can check the value the returned column user_id. If it's NULL then it was not liked by the user, if it contains the user's id, it was liked by the user.
I think u can retrieve the entries and query the likes table at the same time to get if the current user likes the entry performing a stored procedure. So u can control the value of the set of data returned by the query for example returning one colum for the entry text and one boolean column to evaluates the current user likes... In this way you will at least one parameter for the stored procedure to indicate who is the current user
I hope this idea help u...

Categories