Complex Query for Displaying comments based on friends added - mysql

I have a very big problem with my website. I have a form, more like a text box used in submitting comments, that not the problem. The problem is displaying the comments. I want to display comments based on the friends a user added like the way facebook, twitter does.
I have 3 tables:photo_tb, comment_tb, friendship_tb.
The friendship_tb contains columns name, friend. name stores your email and friend stores the email of the person you add....
I need the photo_tb to get the picture of the person making the comment and I use a mysql join successfully.
In my comment_tb...i get the email of the person making the comment in my commentname column and comment made by the person...
I want to to show comment by ffriendship like if Mr A adds Mr B and Mr c..when Mr A logs on he should only get comments by both Mr B and Mr c and his comments doe not comments from Mr G or H.
This is what i have so far
SELECT
comment_tb.comment_id
, comment_tb.comname
, comment_tb.comment
, comment_tb.time
, photo_tb.name
, photo_tb.photo
, friendship.name
, friendship.member
FROM comment_tb, photo_tb, friendship
WHERE
comment_tb.comname=photo_tb.name
and friendship.name = colname
The colname is a variable for MM_Username ,the session variable when you log in is meant to display comments related to you.
The sql statement attaches the picture to each comment name successfully but doesn't limit the comment to friends added.

Add and friendship.member = comment_tb.comname.
BTW, it would be nice if the column names in your SQL matches what you said in the description. I'm just guessing that friendship.member is what you called friend in the text.

Related

How to create a "Read Message" query from table messages and messages viewed?

I would like to get the messages that someone hasnt read... it could be a count o just a "1" if there are pending messages to read.
The trick is that there are many " users" shareing the same system. So if I usear "A" reads a message from the table then the notification wont appear anymore to A, but for "B" there should be a notification of pending messages. They are sharing the same message lets say.
I create a query that works somehow , but I know is not 100% right.
I did review
Querying conversations from messages table
sql messages table query
In the example below is the deal.
"A" last viewbymessage for the docid 93 was on 2019-01-28 10:02:15, then user B send a new message BUT never reads the message sent by "A", so in my query, "A" will never be able to see there was a new message since he was the last to see if, and I not using the MessageTable only the Messages_View .. I know this is the wrong part, but im just stating how I used to have it.
SELECT B.*
FROM Comments_Viewed_Tbl B LEFT JOIN Comments_Viewed_Tbl C
ON (B.DOCID =C.DOCID and B.Date_Viewed < C.Date_Viewed)
WHERE C.Date_Viewed IS NULL and B.viewedby <>'A' and
B.RPDOC = 93 and B.Country ='USA'
*sorry for the image, I did try to put it as text but the system format irt ugly
How would be the best approach to do the query.
In this scenario A should have an alert or counter of the new message as also B since he/she didnt check it and just send a new one.
So adding a comment is the same as sending a message?
From my point of view, you need to add the CommentID column to the Comments_Viewed_Tbl, otherwise you will never be able to see the read status of each specific comment, only for the whole document.
Otherwise you will need to assume that the last person to add a comment to the document has read all previous comments.

I can't find a way to implement messages in my school database on MySQL

I'm working on a school database on which I would like to implement messages that will be created by the schools for the parents to view.
The workflow goes like this:
1. The school sends a message to a certain group of students, it could be a message to all the students from that school, or a message to just the first year, or a message to classroom 1B (1 being the year and B the group), or even a message to just 1 student.
2. Parents access a platform on which they will see the messages regarding their children.
For example:
if the school sends a message to the classroom 1B, only parents with children on that classroom will be able to see it.
if the school sends a message to the first year, only parents with
children on the first year will see it.
What I need help with is:
How could I arrange the database in order to accomplish the message
filtering (By school, by year, by classroom (1B, 2A,
etc.) and by student)?
What would be the sentence that I need to use in order to retrieve
the messages for a parent regarding their children?
I hope I explained myself well, please feel free to ask any question you have, and thank you so much :)
Here's a pic of the database:
If I understood well, for this question "What would be the sentence that I need to use in order to retrieve the messages for a parent regarding their children?" you could use a simple inner join between message and parent_detail on id_students are equals where id_parent is your parentID:
SELECT * FROM `message` m
INNER JOIN `parent_detail` p_d on p_d.ID_student = m.ID_student
WHERE p_d.ID_detail = 'parent_id_variable'
Regarding the first question, using the same principle, you need to use an inner join between message students and schools (if you want the name of the school and not only the ID) and apply in where condition what parameter you want.
For example, by school => message.ID_school, by school and by year => message.ID_school and students.year.

Mysql: Conditional select / concat depending on column matches

I have a database with user details in one table and linked contact details in another table (where the contact details are stored as "content").
I am trying to make a quick search function where you can search for the name or any contact details. So you can either search for name or an email or phone (whatever is in the contact detail field).
This is what I have so far:
SELECT DISTINCT leads.id, CONCAT(first_name,' ', last_name) AS name
FROM `leads`
INNER JOIN `contact_details` ON contact_details`.`lead_id` = `leads`.`id`
WHERE ((CONCAT(first_name, last_name, content) LIKE ('%[XXX]%')));
This works fine. You can search for f ex "55" and it will return hits on f ex ph number 555-573-3222 or you can search for a name string like 'Joh' and it will match 'Johnson'.
My problem, though, is that regardless of what you are searching for, what is being returned is client name. Since this is for an autocomplete feature, this is obviously very confusing. If you start typing in 555-2 you want to see the suggestion 555-221-6362 not "John Johnson".
How can I return EITHER a phone number or email (from column "content") OR the concact first_name, ' ', last_name depending on whether the search matched a name or a contact_detail.content.
Since I am searching on first_name OR last_name, the search works well for "joh" matching "John" but obviously breaks when you search for "John Stan" for "John Stanley". Is there a Mysql way of fixing this or do I need to clean up string before and do alternative searches if there is a space (searching first_name AND last_name separately)
Any suggestions would be greatly appreciated as I have struggled with this for days now.
Charliez, per our comment conversation, the following is an example of how to do some nested if/thens as well as the break out of the where. You'll need to adjust this to met your specific needs, but should give you enough of an example that you should be able to get things working.
SELECT
IF(last_name LIKE '%JAM%',
last_name,
IF(first_name LIKE '%JAM%',
first_name,
''
)
) AS MatchedFieldText
FROM employee
WHERE
last_name LIKE '%JAM%'
OR first_name LIKE '%JAM%';
1) I don't think there is a way to have a SELECT statement return something different based on an OR. My first thought to solve this would be to return first, last, content and use some regex to determine if you should show the name or the number/content.
2) Kind of a hard problem, and I don't think I have seen a perfect solution to it. This might give you some ideas/put you on the right track.

Append string to another table in MySQL

I’ve got a problem selecting some things in my database.
I’ve got 1 database with a lot of tables, however I only use 2 with this specific task.
I have a Clothing table, and I need to import this in my new database. I have succesfully transferred a lot of data, but now I need to take 1 final small step into completing this. In my Clothing table I have a column called Youtube link, here I have a link with an Youtube link, this specific link, in this specific column, I want to append that to another table I have in the database, let’s call it new_clothing_table. I there have a description column called prod_desc and I want to append the Youtube link to that column.
But there is also another “problem”, it’s not that every product has a Youtube link, so things have to be filtered in order to not fuck things royally up. A advantage I have, I have in both tables a product_name, and these are all the same. So I want to transfer that specific Youtube link if it’s there (sometimes there is a 0 filled in or nothing, but I dont think it’s NULL because if I make a SELECT query where Youtube_link is null I get zero rows..)
So can someone help me out>?
mysql has an update-join construct you can use:
UPDATE new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'
SET nct.description = CONCAT(nct.description, c.youtube_link)
EDIT:
To make sure this does what you want, you could first select the updated content in order to examine it:
SELECT nct.description AS old_description,
CONCAT(nct.description, c.youtube_link) AS new_description
FROM new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'

SQL, finding most recent entry in conversation between two people

I have a table, Messages, which is set out as follows:
This table stores all messages sent between people, however it needs to be like FaceBook's message system.
To do this my query would need to be something like:
SELECT * FROM Messages WHERE To_User = x OR From_User = x ORDER BY Time DESC
This would fetch me every message 'x' either sent or received in order of time, newest ones appearing first.
However Facebook's system keeps it sort of like a forum, so I only want one message where person y is involved in any given query result. So I don't want this to happen:
To_User: 6
From_User: 7
Status: I agree with your message
Time: 12345
To_User: 7
To_User: 6
Status: Do you agree with my message?
Time: 12344
Say I was displaying the messages page for user 6, both messages would show up with my current system.
Instead, only the top one should be selected, because I only want to display the most recent message between user 6 and 7, rather than every one.
Naturally this would usually just ask for a LIMIT statement but I need to be able to get a lot of messages, but the only rule is that:
1- It needs to be either FROM or TO user X
2- No two messages must be selected which are FROM or TO user Y
3- The messages selected must be the NEWEST in the conversation between user X and Y
This is really hard to describe but I hope I've described it in enough detail for a solution.
I've considered doing GROUP BY but I couldn't get it to take into account that it needed to find one that was either FROM or TO me rather than just only to or only from.
If the question isn't described in enough detail, please tell me before down-voting and I'll do my best to make it a bit more detailed, but I can't think of words to describe what needs to be displayed clearly enough.
Edit:
Here's a big example of the desired feature:
Say I sent a message to John Doe, and went on my messages page straight after, at the very top it'll say
"To John Doe"
"The message I sent"
However if John Doe replies, that entry will not display on the page anymore, it'll be replaced by the newest entry in the conversation between me and John Doe, so while the actual entry is still in the database, it isn't shown anymore:
"From John Doe"
"Your message is interesting"
But if I sent one to Jane Doe afterwards, it'd push me and John Doe's conversation so it looks like this:
"To Jane Doe"
"Hello there"
"From John Doe"
"Your message is interesting"
Now with the first query I stated in this post, the result would be:
"To Jane Doe"
"Hello there"
"From John Doe"
"Your message is interesting"
"To John Doe"
"The Message I sent"
However I don't want to display more than one which is between me and John Doe, but since I'm looking at two different columns in the table (From and two) to dictate whether both I and John Doe are related, I don't think a regular GROUP BY statement would work.
Now, if in addition, Bob Joe sent me a message, it might look like:
"From Bob Joe"
"Hello"
"To Jane Doe"
"Hello there"
"From John Doe"
"Your message is interesting"
You can self-join the table and use an outer join, so that any "earlier" messages will "filter out" any "non-earlier" messages.
SELECT * FROM Messages main LEFT JOIN Messages earlier
ON earlier.time < main.time AND
(
earlier.To_User = main.To_User AND earlier.From_User = main.From_User OR
earlier.To_User = main.From_User AND earlier.From_User = main.To_User
)
WHERE (main.To_User = x OR main.From_User = x) AND earlier.id IS NULL
ORDER BY main.Time DESC
If you could eliminate the complication of a person appearing in either the "from" or "to" columns, then this would be an instance of the common problem of finding the minimum row within a group. So you could start with this:
SELECT id, time, to_user as user
UNION ALL SELECT id, time, from_user as user
and then use the results of that query as the input to any of the standard minimum row techniques.