I've been struggle for hours with the follow join issue in Zend Framework.
My table(s)
Table (websites)
id
user_id
website
url
...
Table (users)
id
username
salt
password
...
Table (reviews)
id
website_id (id of website)
user_id (id from the user/owner of the website)
reviewer_id (id from the user who has reviewed the website)
review
...
What do I want to get..
To make a array with join of the review and user data is no problem, but I want also
add the website compare to the review.
I made the follow join but I won't work like I want, I have just 3 test reviews in my database and I'm getting over 12 results in my array.
My query:
$select = $this->_db->select()
->from('reviews')
->joinLeft('users', 'reviews.reviewer_id = users.id')
->joinLeft('websites', 'reviews.user_id = reviews.user_id')
->where("reviews.user_id = $user_id");
$result = $this->getAdapter()->fetchAll($select);
With kind regards,
Nicky
Try adding a groupBy to your query (untested)
$select = $this->_db->select()->from('reviews')
->joinLeft('users', 'reviews.reviewer_id = users.id')
->joinLeft('websites', 'reviews.user_id = reviews.user_id')
->where("reviews.user_id = $user_id")
->group('reviews.id');
Related
I've been working on this code for a wordpress social media site where you can visualize people of your opposite sex only if they are not your friends (if they are your friends they'll go to another page)
In the php I already can divide men from women, but now I want to also eliminate the men/women whom already are your friend
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data, WHERE field_id = 3 AND value = 'homme'";
(with this I would get only men), now the info about their friend status is in another table, I tried using WHERE EXIST to comprobe it
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data, WHERE field_id = 3 AND value = 'homme' AND EXIST (SELECT id {$wpdb->prefix}bp_friends WHERE (initiator_user_id = $user_id AND is_confirmed = 1) OR (friend_user_id = $user_id AND is_confirmed = 1)) ";
But doesn't seems to work.
I just want the user_id from the first table, but if I wanted to extract the friend status (that I dont want to extract, I just want it to corroborate my other info to cut out user_ids) I could apply this query
$already_friends = "SELECT is_confirmed FROM {$wpdb->prefix}bp_friends, WHERE initiator_user_id = $user_id OR friend_user_id = $user_id";
I don't know what is the structure of the tables you are referring to. Based on provided information this might work:
SELECT user_id
FROM {$wpdb->prefix}bp_xprofile_data
WHERE
field_id = 3 AND
value = 'homme' AND
user_id NOT IN (SELECT friend_user_id
FROM {$wpdb->prefix}bp_friends
WHERE initiator_user_id=$user_id AND is_confirmed=1) AND
user_id NOT IN (SELECT initiator_user_id
FROM {$wpdb->prefix}bp_friends
WHERE friend_user_id=$user_id AND is_confirmed=1)
I should acknowledge that this SQL statement looks poor: it is slow and it is hard to read. It should be improved if possible.
I have an hard time to retrieve data in a table from MySQL database.
I have 2 different database that cannot be merge but there is a table in the first database that is identical to the second database.
Description Database 1 table: areas : ar_id, name, password.
Description Database 2 table: user : id, username, pass.
Now, When the user Login, He logs in the 2nd database. in each page of the user I have use $_SESSION['username'] to call the username.
Importantly, In every page, I have table that displays data from different tables using the username in the 2 Database; this else the SQL to be specific and only provide each user with their own information. and That's Ok. This is the SQL:
SELECT Client_table.Name, Client_table.Client_Id FROM Client_table, user WHERE user.username = '" . $_SESSION['username'] . "' AND Client_table.Branch = user.area Order by Name ASC
In one of the page, I totally using the 1st Database with this SQL to display data in the table :
select site_id, site_name from sites WHERE srep_id = 5
AND status = 1 or status = 2
order by site_name asc
QUESTION: I would like to display this SQL data in a table by using the username or id from the 2nd database BUT is returns Empty Table (I include both Database in this page). This is my current SQL but still not displaying anything:
SELECT cl.client_name, st.site_id, st.site_name
FROM Database1.sites st
JOIN Database2.user u ON u.id = st.ar_id
JOIN Database1.clients cl ON cl.client_id = st.client_id
WHERE Database1.st.name = '".$_SESSION['username']."'
AND st.status > 0
ORDER BY st.site_name ASC
NOTE: This is a major problem that took me almost a week!
Please some one help!
I think I have an answer.
After browsing and doing some search, I sound that I can make use of the $_SESSION here and Also, This was my final SQL Statement that Helped me to Connect the 2 Database from the same SQL Statement by using variable in PHP Script.
session_start();
$result = mysql_query("SELECT cl.client_name, st.site_id, st.site_name, ar.rep_id
FROM sites st
JOIN areas ar ON ar.rep_id = st.srep_id
JOIN clients cl ON cl.client_id = st.client_id
WHERE st.srep_id = '".$_SESSION['userarea']."'
AND st.status > 0
ORDER BY st.site_name ASC");
I've been using Grocery Crud to develop a simple local application that allows users to register themselves and like bands and rate them and select people they know that are also registered in the application.
Entities:
Person(person_id,person_URL, fullname, hometown)
Band(band_id,band_URL,band_name,country,genre)
Relationships:
Likes(person_id,band_id,rate)
Knows(person_id,known_person_id)
My questions are:
1) I want to return a table of person and known person like below:
KNOWS
person_id | fullname | known_person_id | known_fullname
but I can't use *set_relation_n_n* function 'cause the relationship is (Person -> Likes -> Person), so it's giving me error. The other solution I came up with is making a custom table making a query to return the values I want and show it in the table (code below). The custom table returned is correct but when I render it to my Grocery Crud table, I need to specify $crud->columns('person_id', 'fullname', 'known_person_id', 'fullname'), and it cannot differentiate the fullname of the person and the fullname of the known person. How would I make it in order to be able to show the table that way?
2) I have the same issue in another table but could manage that using the function *set_relation_n_n* 'cause it's a relationship (Person -> Likes -> Band), so since it's 2 different entities it didn't return me a error. The problem is that the query (code below) returns me the whole table and I want only 25 records per page. When I try to use "LIMIT 25" in the query, it returns me ONLY 25 records and the "next page" button doesn't work. Any solutions?
Below, all the information:
CODE for question 1:
function knows_management()
{
$crud = new grocery_CRUD();
$crud->set_model('model_socialnetwork');
$crud->set_table('knows');
$crud->set_subject('Known');
$crud->basic_model->set_query_str('SELECT tb1.person_id, tb1.fullname, tb1.known_person_id, person.fullname FROM (SELECT person.person_id, person.fullname, knows.known_person_id FROM person INNER JOIN knows ON person.person_id = knows.person_id) tb1 INNER JOIN person ON tb1.known_person_id = person.person_id');<br>
$crud->columns('person_id','fullname','known_person_id','fullname');
$output = $crud->render();
$this->_socialnetwork_output($output);
}
CODE for question 2:
function likes_management()
{
$crud = new grocery_CRUD();
$crud->set_model('model_socialnetwork');
$crud->set_table('likes');
$crud->set_subject('Like');
$crud->columns('person_id','fullname','band_id','band_name', 'rate');
$crud->basic_model->set_query_str('SELECT tb2.person_id, tb2.fullname, tb2.band_id, band.band_name, tb2.rate FROM(SELECT tb1.person_id, person.fullname, tb1.band_id, tb1.rate FROM(SELECT person.person_id, likes.band_id, likes.rate FROM person INNER JOIN likes ON person.person_id = likes.person_id) tb1 INNER JOIN person ON tb1.person_id = person.person_id) tb2 INNER JOIN band ON tb2.band_id = band.band_id');
$output = $crud->render();
$this->_socialnetwork_output($output);
}
Question 1) What if you use an alias name in your query, for example
SELECT tb1.person_id, tb1.fullname as Tb1fullName, tb1.known_person_id, person.fullname as PersonFullName
Question 2) I would not recommend you to add LIMIT directly / manually in your query. In the file application/config/grocery_crud.php, you have two options directly related to pagination
You should use and configure them properly
// The default per page when a user firstly see a list page
$config['grocery_crud_default_per_page'] = 25;
....
//Having some options at the list paging. This is the default one that all the websites are using.
//Make sure that the number of grocery_crud_default_per_page variable is included to this array.
$config['grocery_crud_paging_options'] = array('10','25','50','100');
Ok so im working on my first ever php/mysql project having come from a software position. I am learning codeigniter and i have worked out that this mysql join will get me friends statuses based on an user.id, how do i add in all my posts to that do i have to do an AND query?
select * from friendships
join users on users.`id` = friendships.`friend_id`
join statuses on statuses.`user_id` = users.id
where friendships.`user_id` = 2
ORDER BY statuses.`id` desc
Any help greatly appreciated
Join query in codeigniter can be written as:
$this->db->select("*");
$this->db->from("friendships");
$this->db->join("users","users.id = friendships.friend_id");
$this->db->join("statuses","statuses.user_id = users.id");
$this->db->where("friendships.user_id",2);
$this->db->order_by("statuses.id","desc");
$result=$this->db->get();
or
$this->db->select("*");
$this->db->join("users","users.id = friendships.friend_id");
$this->db->join("statuses","statuses.user_id = users.id");
$this->db->where("friendships.user_id",2);
$this->db->order_by("statuses.id","desc");
$result=$this->db->get("friendships");
Well, CodeIgniter abstracts DB interaction through ActiveRecord (so writing full SQL query strings really isn't necessary). They do a better job of explaining it than I could:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Why not just add each user as a follower of themselves in your friendships table? So on registration you add them to users table and to the friendships table.
User 1 follows User 1 etc
You can also use this query in codeigniter
$query = $this->db->query("select * from friendships
join users on users.`id` = friendships.`friend_id`
join statuses on statuses.`user_id` = users.id
where friendships.`user_id` = 2
ORDER BY statuses.`id` desc");
$result = $query->row_array();
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