i have three tables
Users :
id
username
password
Second table
Friends
id
myid
friends_id
and last one
CI_Sessions
session_id
User_id = default =0
User_data
i want Codeignier Query
select * from users where userid=$userid
select * from friends where myid=$userid or friend_id=$userid
select * from ci_sessions where user_id=$userid and user_data!=''
return result()
is possible to get all three in one statement ?
i was trying like this
function check_friends($username) {
$this->db->where('username',$username);
$q1 = $this->db->get('users');
$myid=$q1->row('userid');
if($username !='' ){
$this->db->select('*');
$this->db->from('users');// I use aliasing make joins easier
$this->db->where('userid',$myid);
$this->db->join('myfriends AS friends_tpl', 'friends_tpl.myid = '.$myid.' or friends_tpl.fid='.$myid.'', 'FULL');
$this->db->join('ci_sessions AS B', 'B.id_unite = A.userid', 'INNER');
$this->db->where('B.user_data !=','');
$this->db->where('B.id_unite !=','0');
$query = $this->db->get();
return $query->result();
}else{
}
}
Update
i try this query
$query = $this->db->query("select * from users as u inner join myfriends as f on f.fid = '".$this->session->userdata('userid')."' or f.myid= '".$this->session->userdata('userid')."' inner join ci_sessions as c on c.id_unite = u.userid where u.username != '$username' GROUP BY u.userid ");
but there is problem , this query not correct result as i want ,
this is friends table value
id | myid | friend_id
1 | 50 | 54
2 | 57 | 50
now if three users login , the query will show that user 54 is friend with user 57 i don't know w7y
Do like this
$query = $this->db->query( 'SELECT * -----
UNION ALL
SELECT * -----
UNION ALL
SELECT *---' );
this will give all result...
Related
I have a table which includes 130k+ rows. I want to list most liked users.
I try lots of queries but I didnt get result.
table 1 : "users" / user_id / user_name
table 2 : "likes" / datetime / user_id
$query = mysql_query("SELECT COUNT(*) FROM
(
SELECT DISTINCT a.user_id, b.user_id,b.user_name
FROM dbo.likes AS a
INNER JOIN dbo.users AS b
ON a.user_id= b.user_id
) AS subquery;");
and
while($row = mysql_fetch_array($query))
{
echo $row=['user_name'].'-'.$row=['count(*)'];
}
select user_name, count(user_name)
from users, likes
where users.user_id = likes.user_id
group by user_name
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
I have 4 tables in MySQL to join. Example
$select = $db->query("SELECT *
FROM ads
WHERE ad_pic='1'
AND ad_status = '1'
ORDER BY ad_id DESC LIMIT 0,4");
while ($fetch = $db->fetch($select)) {
$iquery = $db->query("SELECT *
FROM images
WHERE img_ads_id = '" . intval($fetch['ad_id']) . "'
AND img_status = '1'
LIMIT 1");
$thumb = $db->fetch($iquery);
$uselect = $db->query("SELECT *
FROM users
WHERE user_id = '".intval($fetch['ad_userid'])."'
AND user_status = '1'
LIMIT 1");
$ufetch = $db->fetch($uselect);
$cselect = $db->query("SELECT *
FROM category
WHERE cat_slug = '".safe_func($fetch['ad_category'])."'
LIMIT 1");
$cfetch = $db->fetch($cselect);
}
I want to know the way to join these in one select statement.
ads table
ad_id ad_userid ad_category
-------------------------------
1 2 5
images table
img_id img_ads_id
-------------------------------
1 1
users table
user_id user_name
-------------------------------
2 John
category table
cat_id cat_name
-------------------------------
5 Vehicles
SELECT *
FROM ads AS a
LEFT JOIN images AS i ON i.img_ads_id = a.ad_id AND i.img_status = 1
LEFT JOIN users AS u ON u.id = a.ad_userid AND u.user_status = 1
LEFT JOIN category AS c ON c.cat_slug = a.ad_category
WHERE a.ad_pic = 1
AND a.ad_status = 1
ORDER BY a.ad_id DESC
LIMIT 0,4
If an ad must have an image, user, or category, you may use a JOIN instead of a LEFT JOIN. If an ad can have more than one image, user, or category, you will need a more complex query.
I would like to make a list to show the user´s friends, that are online.
users
id | firstname | lastname | last_access
users_friends
uID | bID | type | accepted
That is how my tabels look like.
Firstly I have the user´s id in $USER.
The $USER´s id is stored in a row, in users_friends, at column "uID". bID is the id of the friend.
I also need to check for accepted, if it's 1, else the user is "waiting" and havnt been accepted by the friend yet.
type should also be checked for "friend" as Im having other types there too.
To check whether they are online or not: last_access containing an unix timestamp.
500 seconds since last access, is what I call "online" in the community.
Now that is all you would need to know. I tried building the query myself and came out with this:
$query = mysql_query("
SELECT *
FROM users u, users_friends uf
WHERE
u.last_access > UNIX_TIMESTAMP()-500
AND
uf.uID = '$USER'
AND
uf.type = 'friend'
AND
uf.accepted = '1'
");
Now i would like to echo the firstname and lastname (from users) of the online friends.
I tried do a while()
while($get = mysql_fetch_array($query)){
echo $get["bID"]."<br>";
echo $get["firstname"]."<br>";
}
I am getting my own user´s name($USER) displayed. But the echo bID is the right id´s for my friends, although it shows all (not filtering out the ones who are online).
How can I do this right, what am I missing?
I think you are missing to specify the relationship between the users and users_friends table
$query = mysql_query("
SELECT *
FROM users u
INNER JOIN users_friends uf on u.id=uf.bid
WHERE
u.last_access > UNIX_TIMESTAMP()-500
AND
uf.uID = '$USER'
AND
uf.type = 'friend'
AND
uf.accepted = '1'
");
Do
$query = mysql_query("
SELECT u.* FROM
users AS u
INNER JOIN
users_friends AS f
ON u.id = f.uID
WHERE
u.last_access > UNIX_TIMESTAMP()-500
AND
f.bID = '$USER'
AND
f.type = 'friend'
AND
f.accepted = '1'
");
This should give you your friends rows. Be aware of SQL injections with that $USER variable though.
Note: This will give you a list of people that are friends to you (you are on their friends list). If "friendship" is not bidirectional and you want to see only friends that are on your friends list you would exchange f.uID with f.bID and vice versa.
SELECT DISTINCT *
FROM users_friends uf
LEFT JOIN users u ON uf.bID = u.ID
WHERE uf.uID = $USER
AND u.last_access > UNIX_TIMESTAMP()-500
AND uf.type = 'friend'
AND uf.accepted = '1'
How do I specify the parent query field from within a subquery in MySQL?
For Example:
I have written a basic Bulletin Board type program in PHP.
In the database each post contains: id(PK) and parent_id(the id of the parent post). If the post is itself a parent, then its parent_id is set to 0.
I am trying to write a mySQL query that will find every parent post and the number of children that the parent has.
$query = "SELECT id, (
SELECT COUNT(1)
FROM post_table
WHERE parent_id = id
) as num_children
FROM post_table
WHERE parent_id = 0";
The tricky part is that the first id doesn't know that it should be referring to the second id that is outside of the subquery. I know that I can do SELECT id AS id_tmp and then refer to it inside the subquery, but then if I want to also return the id and keep "id" as the column name, then I'd have to do a query that returns me 2 columns with the same data (which seems messy to me)
$query = "SELECT id, id AS id_tmp,
(SELECT COUNT(1)
FROM post_table
WHERE parent_id = id_tmp) as num_children
FROM post_table
WHERE parent_id = 0";
The messy way works fine, but I feel an opportunity to learn something here so I thought I'd post the question.
How about:
$query = "SELECT p1.id,
(SELECT COUNT(1)
FROM post_table p2
WHERE p2.parent_id = p1.id) as num_children
FROM post_table p1
WHERE p1.parent_id = 0";
or if you put an alias on the p1.id, you might say:
$query = "SELECT p1.id as p1_id,
(SELECT COUNT(1)
FROM post_table p2
WHERE p2.parent_id = p1.id) as num_children
FROM post_table p1
WHERE p1.parent_id = 0";
You could try something like this
SELECT pt.id,
CountTable.Cnt
FROM post_table pt LEFT JOIN
(
SELECT parent_id,
COUNT(1) Cnt
FROM post_table
WHERE parent_id <> 0
GROUP BY parent_id
) CountTable ON pt.id = CountTable.parent_id
WHERE pt.parent_id = 0
To get back to your example, use the alias of the main table in the sub select
SELECT pt.id,
(SELECT COUNT(1) FROM post_table WHERE parent_id = pt.id)
FROM post_table pt
WHERE pt.parent_id = 0
Give the tables unique names:
$query = "SELECT a.id, (SELECT COUNT(1) FROM post_table b WHERE parent_id = a.id) as num_children FROM post_table a WHERE a.parent_id = 0";
The following syntax works in Oracle. Can you test if the same works in MYSQL too?
It is called scalar subquery in Oracle.
You would just need to alias the two tables differently to distinguish between them if you are using the same table twice.
sql> select empno,
2 (select dname from dept where deptno = emp.deptno) dname
3 from emp
4 where empno = 7369;
EMPNO DNAME
---------- --------------
7369 RESEARCH
sql> select parent.empno,
2 (select mgr from emp where empno = parent.empno) mgr
3 from emp parent
4 where empno = 7876;
EMPNO MGR
---------- ----------
7876 7788
Thanks Don. I had a nested query as shown below and a WHERE clause in it wasn't able to determine alias v1. Here is the code which isn't working:
Select
teamid,
teamname
FROM
team as t1
INNER JOIN (
SELECT
venue_id,
venue_scores,
venue_name
FROM venue
WHERE venue_scores = (
SELECT
MAX(venue_scores)
FROM venue as v2
WHERE v2.venue_id = v1.venue_id /* this where clause wasn't working */
) as v1 /* v1 alias already present here */
);
So, I just added the alias v1 again inside the JOIN. Which made it work.
Select
teamid,
teamname
FROM
team as t1
INNER JOIN (
SELECT
venue_id,
venue_scores,
venue_name
FROM venue as v1 /* added alias v1 here again */
WHERE venue_scores = (
SELECT
MAX(venue_scores)
FROM venue as v2
WHERE v2.venue_id = v1.venue_id /* Now this works!! */
) as v1 /* v1 alias already present here */
);
Hope this will be helpful for someone.
Parent query field within a subquery in MySQL 8.
I'm selecing games scores on the basis of username from tblgamescores using nested query.
SELECT
GameScoresID,
(SELECT Username FROM tblaccounts WHERE AccountID = FromAccountID) AS FromUsername,
(SELECT Username FROM tblaccounts WHERE AccountID = ToAccountID) AS ToUsername,
(SELECT Username FROM tblaccounts WHERE AccountID = WinAccountID) AS WinUsername,
(SELECT Username FROM tblaccounts WHERE AccountID = LossAccountID) AS LossUsername,
FromUserScore,
ToUserScore
FROM tblgamescores a
WHERE FromAccountID = (SELECT AccountID FROM tblaccounts WHERE Username = "MHamzaRajput");