I have written an SQL query which I have to do using CodeIgniter.
My query is:-
SELECT COUNT('user_id') FROM tbl_tickets_replies WHERE user_id IN (SELECT id from tbl_users WHERE username IN (SELECT username FROM tbl_tickets WHERE site_referers_id =1))
I am doing this in my model
function getCommentNumbers() {
$sql = "SELECT COUNT('user_id') FROM tbl_tickets_replies WHERE user_id IN (SELECT id from tbl_users WHERE username IN (SELECT username FROM tbl_tickets WHERE site_referers_id =1))";
return $this->db->query($sql);
}
How this can be done using active Records
Its not working :(
I have three different tables which are:-
tbl_users(id,username);
tbl_tickets(id,username,site_referers_id)
tbl_tickets_replies(id,user_id,comments)
what I want to do is select all comments belonging to particular username having site_referers_id=1.
I thought to select distinct username from tbl_tickets having site_referes_id =1 and then get the id of selected username from tbl_users and use that id to count how many comments he have and display it according to the username.
MY query is not doing so, it is displaying total comments of all users i.e.,
suppose there are two users A and B with users id 1 and 2 having 10 and 15 comments
then it should display like :
A 10
B 15
rather my query is showing
A
B 25
What you're missing is the GROUP BY aggregate function.
Try this:
SELECT DISTINCT user_id, COUNT('user_id') FROM tbl_tickets_replies WHERE user_id IN
(SELECT id from tbl_users WHERE username IN
(SELECT username FROM tbl_tickets WHERE site_referers_id =1)) GROUP BY user_id
Related
I have a database with direct messages from one user to another.
Table name: dm
id int(11) Primary Key
to: varchar
from: varchar
message: varchar
I have a SQL Statement that selects the distinct values from the to and from column as if they were one column. ($username is a session variable)
"SELECT DISTINCT from
FROM dm
WHERE to = '$username'
UNION
SELECT DISTINCT to
FROM dm
WHERE from = '$username'
";
I am checking to see if this user has received or sent any messages basically. I want to ORDER BY id of dm. How can I change my code so I get the same results and it orders by id DESC? If I simply enter ORDER BY like so, I get an error because I didn't select it... Thank you so much for the help
"SELECT DISTINCT from
FROM dm
WHERE to = '$username'
UNION
SELECT DISTINCT to
FROM dm
WHERE from = '$username'
ORDER BY id DESC
";
If I got exactly what you wanted to do you should keep things separate:
1st step: your original code, plus the user ID added to the dataset
CREATE TABLE new_table_name AS
SELECT DISTINCT ID, from
FROM dm
WHERE to = '$username'
UNION
SELECT DISTINCT ID, to
FROM dm
WHERE from = '$username';
2nd step: order by ID
CREATE TABLE ordered_table AS
SELECT *
FROM new_table_name
ORDER BY ID DESC;
I have tables:
likes - id, user_id, like_user_id<br>
users - id, name, email ...,<br>
friends - id, user_id, friend_id, status<br>
Is it possible to sort it with one SQL query first to show the friends then the other users.
Any help would be appreciated.
Thank you.
I tried this and it works, but the problem is it give me double results of users:
select *
from `likes`
left join `users` on `users.id` = `likes.user_id`
left join `friends` on `friends.user_id` = `likes.user_id`
or `friends.friend_id` = `likes.user_id`
where `likes.id` = 1
order by `friends.user_id` = 5
or `friends.friend_id` = 5
You need to work with a UNION here to merge the liked users with the befriended users. Upon doing this, you can create an artificial column friend, that you fill with 1 in the friend query and 0 in the like query. Later on you can order by that column.
SELECT
friends.user_id,
1 as friend,
users.*
FROM
friends
JOIN users ON users.id = friends.friend_user_id
UNION SELECT
likes.user_id,
0 as friend,
users_liked.*
FROM
likes
JOIN users as users_liked ON likes.like_user_id = users_liked.id
WHERE
user_id = '$userId'
ORDER BY friend DESC, id ASC
This will return a list of all friends, followed by a list of all liked users.
I'm trying to make a homepage somewhat like Facebook, I made it so it could show the posts from the people I follow, but I couldn't see my own posts as I can't follow myself. Here is the line of SQL code I've written (it contains PHP variables):
SELECT *
FROM user_posts
INNER JOIN user_following ON user_posts.username = user_following.username
WHERE user_following.follower = '$me->username'
ORDER BY id DESC
LIMIT 0, 15
The user_posts table contains all the posts.
The user_following table contains all follow data, where username is the user being followed, and the follower is the user following the username
$me->username is the username of the user logged in.
user_posts table structure:
user_following table structure:
Thanks, in advance!
There's a couple of different ways to skin this query:
Sub-query
SELECT *
FROM user_posts
WHERE user_posts.username = 'bob'
OR user_posts.username IN(
SELECT username
FROM user_following
WHERE user_posts.username = user_following.username
)
LIMIT 0, 15
http://sqlfiddle.com/#!9/6bf2c6/9
Use the Users Table
Requires GROUP BY or DISTINCT user_posts.id, which are non-optimal.
SELECT
user_posts.*
FROM users
LEFT JOIN user_following ON users.username = user_following.username
INNER JOIN user_posts ON (
users.username = user_posts.username
OR user_following.follower = user_posts.username
)
WHERE users.username = 'bob'
GROUP BY user_posts.id
LIMIT 0, 15
http://sqlfiddle.com/#!9/d91be/1
IMPORTANT! Make sure and index those columns in your table. Otherwise, performance will suffer as the tables get bigger (especially user_following).
Try this code:
select *
from user_posts up
join user_following uf on up.username = uf.username
where uf.follower = '$me->username'
or up.username = '$me->username'
I'm sorry this has probably been answered hundreds of time but I'm totally lost between different scenarios here.
What I want is pretty simple. I have 2 tables "bets" and "users".
In the table "bets", I put the UserID instead of the UserName. In the table "users", the UserName is linked to the UserID.
I would like to be able to read the data from the table "bets" and display the UserName instead of the UserID, so I will need some sort of code to match the UserID contained in the table "bets" and return the UserName instead.
The MySQL query I have for now:
$sql5="SELECT * FROM Bets, Users WHERE GameID = '$NGnumber' ORDER BY DrawOrder";
$result5 = mysql_query($sql5) or die(mysql_error());
while($rows5 = mysql_fetch_assoc($result5)){
...
I can easily echo $rows5['UserID'] but I would like the UserName (in the Users table) instead. How can I do that?
Thanks!
Use inner join:
SELECT * FROM Bets INNER JOIN Users ON Bets.userID = Users.userID WHERE GameID = '$NGnumber' ORDER BY DrawOrder
Replace the query:
SELECT * FROM Bets b INNER JOIN Users u
ON b.GameID = u.GameID
WHERE GameID ='$NGnumber' ORDER BY DrawOrder"
I am trying to join my users table with another table using the following query...
SELECT * FROM (`activities`)
JOIN `users` ON `users`.`id` = `activities`.`user`
WHERE `user_subdomain` = 'hi' OR user_subdomain = ''
ORDER BY `activities`.`id` desc
LIMIT 10
Is there any way to do the join so that the id of the user does not replace the id of the activity?
For example, currently if there is an activity with the id of 10 and the user 2 the id will be replaced by the id of the users table and show as 2 after I run the query.
Thanks a lot for the help!
Whenever you are joining tables, you ought to be explicit about the columns you select rather than using SELECT *, and specify column aliases for them when the same column name is used in multiple tables.
SELECT
activities.id,
activities.othercol,
/* Alias to userid */
users.id AS userid,
users.name,
users.anothercolumn
FROM (`activities`)
JOIN `users` ON `users`.`id` = `activities`.`user`
WHERE `user_subdomain` = 'hi' OR user_subdomain = ''
ORDER BY `activities`.`id` desc
LIMIT 10
Though it isn't strictly necessary to prepend the table name to each, unless the column names are the same.
SELECT
activities.id AS activityid,
othercol,
users.id AS userid,
name,
anothercolumn