Combine 2 columns as 1 with other columns as well - php

I have this table
myid his_id
7 1
1 7
1 6
1 3
But its giving me 4 records instead of 3, i tried using mysqli "join" , "group by" but didnt work.
Im trying to develop a chat system with pure php and jquery, in d table above, user 7 chatted with user 1 and user 1 chatted with user 7 so i want to see it as just the same chat and the rest of the 2 ( user 1 chatting with user 6 and user 1 chatting with user 3)
I want it to give me just 3 records instead of 4 records, pls any help will be appreciated, thank you
Sorry im not connected to my system, im using a smart phone, forgive my bad formatted query below
$sql = mysqli_query($connectn, "select a.id, b.his_id, a.myid, a.status, a.date_replied FROM chats a JOIN chats b ON b.myid=b.myid WHERE b.his_id='$regestered_id' or b.myid='$regestered_id' GROUP BY b.his_id ORDER BY a.id DESC") ;

If you want distinct combinations of myid and his_id, you must not print id, date_replied, status, etc, but just two columns- his_id, myid.
Because if there are multiple rows with two columns with same values, it is yet another to decide which row must be selected in order to print id, status, etc.
So, just to print which two users are chatting, use myid and his_id. You can later use them for any other information.
Now, the code you wanted is-
$sql = mysqli_query($connectn, "select distinct
least(myid, his_id), greatest(myid, his_id)
from chats");
And yes, like #iblamefish mentioned, don't forget to protect your code against SQL injection.

Related

Voting SQL conundrum

I have a PHP system that allows users to vote photos on a scale of 1 - 5, what I want to do is highlight where two people give each other the same vote/score. I can't figure the SQL out at the moment for my PHP function.
The database looks like this
id, user_uid, voted_uid, score
As someone votes the id is auto incremental, the user_id is inserted from the session uid and the voted_uid comes from the image the user is viewing, then the score is the ranking from 1-5.
In theory we are therefore looking for two similar rows like this:
uid user_uid voted_uid score
7 3 5 3
38 5 3 3
At this point I want my php function to take the current users session and then match their votes and scores with other users.
In the example above I'd have the session id of 3 and I want it to return these two records as matches.
If I understand you correctly, what you want is to find pairs of rows where user_uid of the first row equals voted_uid in the second row and vice versa. But only, if score is the same in both rows.
In that case, this should do the trick:
SELECT a.*
FROM table AS a
JOIN table AS b
ON a.user_uid = b.voted_uid
AND a.voted_uid = b.user_uid
AND a.score = b.score;
If you only want rows that "mention" a specific uid, you of course have to add a WHERE user_uid = 3 OR voted_uid = 3.

MySQL search different tables and only fetch specfic IDs

I am trying to create an algorithm to sort out the most relevant data for a specific user_id.
I imagine my end result to be an array with KEY as the found USER_ID and the VALUE to be the number of times the specific USER_ID has been found in the different rows.
So, I need to look through different rows in different tables and look for where CURRENT_USER_ID (lets say id: 30) exists, and then find the RECIEVER_ID, that is the user which was communicated to. This is pretty hard to explain, but lets take an example:
I have a table called: edu_posts, which contains wallposts and comments to theese. The are different values in this table, but the ones we should focus on is: post_author and post_reciever. We then have to look for all the rows where post_author equals 30 (the test example; just needs to be the current users id) and then print out the post_reciever IDS. This would be easy enough with a single query, but lets say we have to find data in 5 or 10 different tables, that 10 different queries, which is a lot.
We also have a table called edu_folowers. There we have to look for where follow_author equals 30 (the test example; just needs to be the current users id), and then print out the follow_user ID. Again, to find out who the current user have interest in.
I image the final mysql_fetch to look something like this:
user_id => 25
times_found => 5
user_id => 11
times_found => 3
user_id => 95
times_found => 1
etc.
Can this be done using a single query, maybe using JOIN? And even maybe count the results IN the query, so I don't have to do this manually in the PHP code.
Or should I create a mysql_query for every table I wish to get data from, and then manage the data afterwards using PHP? This sounds like the easiest way to me, but also the most inefficient relating to script optimization.
I have tried out with the following test-query:
SELECT
u.user_id AS user_id,
f.follow_user AS user_id_follow,
p.post_reciever AS user_id_posts
FROM
`edu_posts` u
LEFT JOIN `edu_followers` f ON f.follow_author = '30'
LEFT JOIN `edu_posts` p ON p.post_author = '30' && p.post_reciever != '30'
WHERE
u.user_id = '30'
GROUP BY
f.follow_id, p.post_id
But the problem is that it outputs unexcepted results, and also I will have different values to look for, fx: user_id (not really needed, as we already know that it is 30), user_id_follow, user_id_posts, and so on.
I hope you understand my question, and please let me know, if you need additional information.
Many thanks in advance!
You could create a union view of all of the tables that you want to search, depending on your exact requirements you might only need to query that once.
e.g.
create view allPostTypesUnion as
select user_id, post_receiver
from edu_posts
union
select user_id, post_receiver
from different_edu_posts
union
select user_id, post_receiver
from another_different_edu_posts
then:
select post_receiver, count(*)
from allPostTypes
group by post_receiver

write a php to output the query result of friends who's friends with user id is 2

I have two column table friends table where it has a recode of users whos friends with eacho other. i want to get a list who's friends with a specific user. to do that i wrote a sql query as such and got a result..
SELECT *
FROM `friends`
WHERE `user_idf` = '2'
OR `user_idff` = '2'
++ user_idf ++++ user_idff+++
2 + 1
3 + 2
4 + 2
2 + 5
+++++++++++++++++++++++++++
how to write a php function to echo user who's a friend of 2 (this i will replace with a POST where it gets user id from an application)
i.e.
1, 3, 4, 5
do you mean something like this?
(SELECT `user_idf` FROM `user` WHERE `user_idff` = 2) UNION (SELECT `user_idff` FROM `user` WHERE `user_idf` = 2)
Im not great with sql to be honest, but I think union is what you are looking for (and I think my syntax is correct although im not 100%).
This should from my understanding take the user ids of anyone who is friends with user 2 and output it. The union of results returned when user 2 is in user_idf and user_idff columns.
EDIT: Actually I still stand by my answer, if you don;t do this sql side then you will have to have a more complex PHP function.
For the PHP itself look into mysqli and PDO as your two main options for prepared statements.

How do I check for logged in user id and return that row first in a MySQL query?

The query below selects the 'loves' on an item. (think of it as similar to facebooks 'like' system.
There are two tables in use in this select. A link table (containing itemid, userid, lovetime) and this is joined to a users table in order to retrieve the username/user profile url etc.
$lovequery = "select love.lovetime, love.userid as ID, love_users.display_name, love_users.user_url
from ".$wpdb->prefix."comment_loves love
left join ".$wpdb->prefix."users love_users on love_users.ID=love.userid
where commentid = $itemid
order by love.lovetime desc
limit 4
";
The results are limited to 4 because I simply do not need any more data. The total count is stored separately in the actual item table to reduce queries.
Once the rows are retrieved from this query I iterate through the array, cross referencing against the total 'lovecount' and build a text string formatted like so:
You, John Smith, Joe Bloggs and 4 others love this.
This works fine however it fails if the logged in user (YOU) does not have the most recent 'lovetime'
What I want to do is have the currently logged in use always at the top of the returned results even if his/her 'lovetime' is older than the 4 most recent ones so that the string always begins with 'You' if the logged in user has 'loved' this item.
The logged in user id is available in the script as $userid.
To clarify
if I have the following table (the timestamps are written as simple UK dates for legibility purposes):-
userid commentid lovetime
34 3 02/10/2011
24 3 03/10/2011
13 3 06/10/2011
65 3 14/10/2011
1* 3 10/09/2011
* with userid 1 being the logged in user id
I would only get user id's 34,24,13,65 returned in that order due to ordering by 'lovetime'
What I want is for the results to return ideally 1,34,24,65. if that proves too tricky then getting 5 total rows when the userid exists would be okay also.
I hope this is clear enough, it was rather difficult to articulate.
How would I go about modifying the query to ensure the results are as described.
Many thanks.
You can order result by condition like ORDER BY (ID = "auth_user_id") DESC

PHP MySQL. Linking 2 Tables columns together

I've got an small issue, I'm trying to make my own forum, but I am stuck at something.
I have 3 tables, 1 users with a user_level (Authentication level).
forum_section which contains all the sections with a user_level again.
But how can I link forum_section.user_level to forum_topics.user_level.
So if I define: forum_section.user_level = 4 for forum_section.section_id = 1.
For example:
Someone wants to visit the forum_section id 1 then they must have a auth level of 4.
And when they go to the topic it then again checks if the user level is 4, but I do not want to manual set the topic level, topic level must always be the same as the section level.
I've googled for this, but I can't really find a good manual for it. I guess it has something to do with: "foreign keys"?
Okay, let me explain a bit more just to be sure.
I have 3 tables
Table User
username = Wesley
userlevel < for example 4(admin).
Table section
section_id = 3
section_name = News
section_level = 3 <require level to view.
table topics
topic_id = 123
topic_name = I like candy
topic_level = Needs to be the same as section_level so if I change section_level it automatically changes this too.
You should have to manage your database by using master and transaction tables.
Your table format should look like this:
table name: user
user_id user_name
1 abc
2 xyz
3 pqr
4 new
table2 name : forum_section
forum_id user_id forum_name
1 4 abc
2 4 jkl
3 2 cbd
4 3 lmn
Now, you can JOIN these two tables and make a query as you want, like this:
SELECT forum_id FROM forum_section as fs,user as u WHERE fs.user_id = u.user_id AND fs.user_id = 4
This may help you. Please write if you need more help!
You need to either use a SQL JOIN or a NESTED SUB QUERY
The simplest implementation would be to initially establish the users access level, then filter subsequent queries based on this- so in your PHP, pass the access level to subsequent requests for content- if the query returns anything, the user can view the content, if not- they can be redirected.

Categories