I have to try two joins but this is not going to work.
code in Codeginator.
$this->db->select('concat(firstName," ",lastName) as fullnameVisitor,users.firstName,visitorId,destinationId,count,activityStatus,created,updated');
$this->db->from('interestedprofile');
$this->db->join('users','users.userId=interestedprofile.visitorId','LEFT');
$this->db->join('users','users.userId=interestedprofile.destinationId','LEFT');
$this->db->join('interestedprofile','interestedprofile.destinationId=users.userId','LEFT');
$this->db->order_by('created','desc');
return $this->db->get()->result_array();
User Table : https://i.stack.imgur.com/ppM3s.png
Interest Table : https://i.stack.imgur.com/EbQIX.png
i have to find visitedid and destinationid users firstName and lastName in Users Table.
You don't have to use the Active Record fully to do the joins. You can use regular SQL. For example,
$query = $db->query("SELECT * FROM users;");
foreach ($query->getResult('User') as $user)
{
echo $user->name; // access attributes
echo $user->reverseName(); // or methods defined on the 'User' class
}
This Code is Work For me.
Select us1.firstName,us2.firstName from interestedprofile ip join users us1 on us1.userid = ip.visitorId join users us2 on us2.userId = ip.destinationId
Related
I have a table with users and one with labels
A label can have many users and a user can have many labels, so a Many to Many relationship
A joining table is needed, that's why I have label_user
Below you can see pictures of what they contain with example data:
Users:
https://i.stack.imgur.com/E5E6O.png
Labels:
https://i.stack.imgur.com/1NFjq.png
label_user:
https://i.stack.imgur.com/tW2Uo.png
Let's say I have 5000 users and I can sort them by gender. Let's say 2800 of them are males, how can I assign them all to a label?
Here's some things I tried:
public function add_users_to_label($label_id, $condition, $value)
{
$db = new Database();
$conn = $db->db_connect();
$label_id = escape_string($conn, $label_id);
$query = $conn->query("INSERT INTO `label_user`(`label_id`, `user_id`) SELECT :label_id, psid FROM `iris_messenger_users` WHERE $condition = $value");
$query->bind_param("iss", $label_id, $condition, $value);
if ($query->execute()) {
return true;
}
else {
return "Error inserting data: " . $conn->error . "\n";
}
}
On the user side I have a simple form with select that let's you select a label and then this code:
if(isset($_POST['label-select'])) {
if ($_GET['show_only_gender'] == 'male') {
$condition = 'gender';
$user->add_users_to_label($_POST['label-select'], $condition, $_GET['show_only_gender']);
}
}
Basically, I want to get all users that are male and assign them to a label and put that into label_user with respectively the label_id and the user_id(psid)
Even if this worked I'd still have to do it 2699 times more. What can I do here to optimize and make it to run with 1 query if possible?
I don't think using foreach and running it as much times as there are users is the best option, is it?
Is there any better approach I can take to make this possible?
Although what you are describing does not make sense to have a "label" associated with a person for this specific component, the gender is already on the user table you should be able to get all male based on
select * from user where gender = 'male'
no need to JOIN to a label table on this field. Similarly if you were trying to find people based on a name starting with something... you would not create a label for the name either. Query directly from the table that has that specific component association.
Now, to answer your question, how to insert into the label table for each instance in bulk, you could do something like... I am doing this based on some label ID = 123 as just an example in your labels table that represents gender.
I am doing a LEFT-JOIN in the select so we dont try to add for any user IDs that are already on file do not try to get re-added.
insert into label_user
( label_id,
user_id )
select
123 as label_id,
U.id as user_id
from
users U
left join label_user LU
on U.id = LU.user_id
AND LU.label_id = 123
where
U.gender = 'male'
AND LU.user_id IS NULL
You obviously need to adjust for php.
I have a table named users and has a user_id, and a table named groups and has a group_id and also have user_id that is a foreign key reference from users's user_id.The situation is here: if the user joined a group, his/her user_id is inserted into table groups. So if the user joined two different groups, the column 'user_id' in table 'groups' will insert two or more same user_id's. Well, I just want to bring the user_id once, either he/she joined two or more groups..
I have no idea how to loop it properly without getting user_id that is the same.... I just want it to loop once...
$query_groups = mysql_query("SELECT * FROM groups");
while ($rows_g = mysql_fetch_assoc($query_groups)) {
$g_user_id = $rows_g['user_id'];
$query_users = mysql_query("SELECT * FROM users WHERE user_id='$g_user_id'");
while ($rows_u = mysql_fetch_assoc($query_users)) {
echo $rows_u['user_id'];
}
}
change your code as follows:
$query_groups = mysql_query("SELECT user_id FROM groups LEFT JOIN users ON users.user_id = groups.user_id GROUP BY groups.user_id");
while($rows = mysql_fetch_assoc($query_groups))
{
echo $rows['user_id'];
}
You are using $rows_g but the variable is namend $rows in the first while loop.
Wrong:
$g_user_id = $rows_g['user_id'];
Correct:
$g_user_id = $rows['user_id'];
But try to use joining tables, because this is an inefficient way to get the wanted data.
In your case you should use LEFT JOIN.
Im trying to find an efficient way to display all posts from people who are being followed by the logged in account holder.
There are two key tables:
1- Posts
table name : posts
id, account_name, published, body
2- Follows
Table name : follows
id, account_name, followed_name
I'm trying to find a way that i can display all the posts from all the accounts that are being followed. The connection between Posts and Follows is the Account_name.
I understand that it will probably be a join, but it's how I construct the WHERE clause. So far I have the following (The account name is set via $_SESSION['account_name']):
$sql = "SELECT * FROM posts LEFT JOIN follows ON posts.account_name = follows.account_name WHERE --- How would I only get the posts from the accounts being followed ?---"
I'm sure this is something simple my brain just feels drained and I cant seem to work it out.
UPDATE Attempting in PDO
Returning NULL at the moment,
$sql = "SELECT * FROM share_posts WHERE account_name IN (SELECT followed_name FROM $this->account_follows WHERE account_name = :account_name)";
return $this->AC->Database->select($sql, array('account_name' => $account_name));
The goes to my Database Class:
public function select($sql, $array = array(), $fetch_mode = PDO::FETCH_ASSOC)
{
$stmt = $this->AC->PDO->prepare($sql);
foreach ($array as $key => $value)
{
$stmt->bindValue("$key", $value);
}
$stmt->execute();
return $stmt->fetchALL($fetch_mode);
}
The returned data is NULL at the moment even though the logged in account has followed other accounts.
$account = $_SESSION['account_name'];
//do some sql injection checking on $account here
$sql = "SELECT * FROM posts WHERE account_name IN (SELECT followed_name FROM follows WHERE account_name='".$account."')";
This will get all the posts where the account name matches somebody you follow. I wasnt sure who was following who, but in this case the followed_name are the people account_name is following. If thats the other way around, switch the values
$sql = "SELECT * FROM posts WHERE account_name IN (SELECT account_name FROM follows WHERE followed_name='".$account."')";
I will write this the way I interpret your question.
What you need to do is select only the posts from the users that are followed by your logged in user.
To break this down, first you want to select the users followed by the logged in user. To do this, we use the Follows table.
We then want to select the posts by these users. As such my query would be this.
SELECT posts.* FROM follows
LEFT JOIN posts ON posts.account_name = follows.follows_name
WHERE follows.account_name = $logged_in_user
I've two tables, users and coach_to_trainee. User can have multiple coaches, and the data is stored in coach_to_trainee columns coach_id and trainee_id.
I'm using coach_to_trainee table to print out data for the user, so he/she can simply see who is his/her coach.
<?php
$user = $_SESSION['login']['id'];
$q = "SELECT * FROM coach_to_trainee WHERE trainee_id='$user'";
$coachid = $db->prepare($q);
$coachid->execute();
while($row = $coach->fetchObject()){
$coachid = $row->coach_id;
echo '<li>'.INSERT_COACH_NAME.'</li>';
}
?>
However, this will only return the ID of the coach, and I need to return the name also, from table users.
How I'm supposed to do another query inside while() and use $coachid to find the correct user?
You have to use JOIN mysql clause, like this one:
SELECT u.name, u.id
FROM users u
LEFT JOIN coach_to_trainee ctt
ON u.id = ctt.coach_id
WHERE ctt.trainee_id = {$user}
You should be done with this one ;-)
Then in Your PHP use the selected values:
while($row = $coach->fetchObject()){
echo '<li>'.$row->name.'</li>';
}
I have a feeling this is really simple. Here's the deal: I have a table with three columns. I want to take all the values in one of the columns and turn that into a list. I want to do this so I can transverse through the list. Each value in the list corresponds to a username. I want to take that username to access info about a user. Using this info, I can check which faculty the user is in and sort accordingly. This is what I've come up with:
function get_users_by_faculty($faculty) {
global $connection;
$query = "SELECT * FROM owner";
$user_set = mysql_query($query); // ERROR could not establish link to server
confirm_query($user_set);
foreach($user_set as $user) { //ERROR invalid argument
$userFaculty = get_info_by_id($user["ownerId"], "ou");
if($faculty == $userFaculty){
return $user["name"];
} else {
return NULL;
}
}
I've been quite stuck on this for a few hours.
I don't know your fields names, but I think you could do that with an sql query.
something like that :
SELECT user.id,user.name, faculty.name
FROM user inner join faculty on faculty.id = user.faculty_id
WHERE faculty.id=?
You should replace ? with your faculty id.
If you want a list of user names, you can use group concat :
SELECT GROUP_CONCAT(user.name SEPARATOR ';')
FROM user inner join faculty on faculty.id = user.faculty_id
WHERE faculty.id=?
GROUP BY faculty.id