functions.php
function getfriendone($id, $field){
$query = mysql_query("SELECT `$field` FROM `friends` WHERE `user_one`='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
function getfriendtwo($id, $field){
$query = mysql_query("SELECT `$field` FROM `friends` WHERE `user_two`='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
index.php
$fetch_friends = $_SESSION['user_id'];
$two = getfriendone($fetch_friends, 'user_two');
$three = getfriendtwo($fetch_friends, 'user_one');
$result = mysql_query("SELECT * FROM posts WHERE id IN ('$two', '$three')");
echo $two." | ".$three;
Table (friends) for getfriend function
ID user_one user_two
1 1 2
2 1 3
3 4 1
Table (posts) for $result
ID TEXT NAME
1 Hello Bob
1 Hello Bob
2 Hello Mark
What i wanna do here is echo all the rows that has integer 1 in it in both user_one and user_two. In my chase, only ID 1 and 3 will be echoed here. It picks the first match and echos only that one. It works as it should, but only with the first match in the column
If i read properly:
SELECT *
FROM posts s
WHERE
EXISTS
(SELECT 1 FROM posts s2 WHERE s.id = s2.user_one) OR
(SELECT 1 FROM posts s2 WHERE s.id = s2.user_two)
Will extract the data you need
SQLFiddle -> http://sqlfiddle.com/#!2/9ae070/3/0
Related
I have a query
SELECT *
FROM `user`
WHERE NOT
EXISTS (
SELECT *
FROM roleInEvent
WHERE user.userId = roleInEvent.userId
AND eventId = 1
)
AND user.disciplineId =5
Whenever I run this in my mysql console it returns 1 row. This is correct.
However in my php script it returns zero rows while it is exact the same query.
My php script looks like this:
$db = DatabaseHelper::get();
$st = $db->prepare('SELECT *
FROM `user`
WHERE NOT
EXISTS (
SELECT *
FROM roleInEvent
WHERE user.userId = roleInEvent.userId
AND eventId = 1
)
AND user.disciplineId =5');
$st->execute();
if ($st->errorCode() !== \PDO::ERR_NONE) {
return null;
}
Somebody knows what is the problem here?
The solution was easy.
I forgot to return the results to my javascript
You can use too the IN with a NOT like:
SELECT * FROM user WHERE id_user NOT IN ( SELECT other_data FROM roleInEvent WHERE user.userId = roleInEvent.userId AND eventId = 1 ) AND user.disciplineId =5
If the query: SELECT other_data FROM roleInEvent WHERE user.userId = roleInEvent.userId AND eventId = 1 bring to you something, this result not have to exist in the user table.
Other example is:
Users Table:
---------------------------
id_user | name_user
1 fernando
2 urban
And in other table we have the block users:
Block Users:
id_user_block | id_user | name_user
1 1 fernando
2 2 john lennon
So,i want bring to me only users that not exists in the block users table:
The query will be:
SELECT * FROM Users WHERE id_user NOT IN ( SELECT id_user FROM blocked_users )
So, the last query we are getting:
id_user | name_user
2 john lennon
Greetings!
I have two table which contain have data like as mentioned below:
Table1
TID Name
1 Salman
2 ABC
3 XYZ
Table2
SID STID SUBJECT
1 1 English
2 1 Math
3 2 Physics
4 2 Math
Table1 TID foreign key in Table2 STID. I want to collect data from two table and display in PHP as mentioned below:
Name Subject1 Subject2
Salman English Math
ABC Physic Math
there is my suggestion algorithm
$mysqli = new mysqli("HOST", "USER NAME", "USER PASS", "YOUR DATABASE");
$result = $mysqli->query("SELECT * FROM table1");
while ($row = $result->fetch_assoc()){
$TID = $row['TID'];
$Name = $row['Name'];
$result2 = $mysqli->query("SELECT * FROM table2 where `TTID` = $TID");
echo "$Name \t";
while ($row2 = $result2->fetch_assoc()){
$SUBJECT = $row2['SUBJECT'];
echo "$SUBJECT \t";
}
echo '<br/>';
}
$mysqli->close();
If you know the maximum number of subjects that can be assigned to a student,
Please check this query
SELECT A.Name ,
(SELECT subject from Table2 where STID = A.TID limit 0,1 ) as SUBJECT1,
(SELECT subject from Table2 where STID = A.TID limit 1,1 ) as SUBJECT2
FROM Table1 A
Is there any way to make a single query statement?
left_table
id|group_id|member_id
---------------------------------
1|10|100
2|13|100
3|14|100
4|13|103
5|14|102
right_table
id|user_id|group_name
-----------------------------------------
10|100|hundred
11|101|hundredone
12|102|hundredtwo
13|103|hundredthree
14|104|hundredfour
$query_id = 100
$results = mysqli_query($link, "SELECT * FROM right_table rt, left_table lt WHERE rt.user_id != '$query_id' and rt.id = lt.group_id and
lt.member_id != '$query_id'");
while ($r = mysqli_fetch_array($results)){
echo $r['group_name'].'<br>';
}
When i query both the table, i only want to retrieve the group name from right table
where my $query_id is not in the left_table(member_id)
desire output for user_id 100 would be hundredone and hundredtwo because the member_id 100 is not part of
any 11 and 12 group from the right_table
SELECT group_name
FROM right_table rt
WHERE NOT EXISTS (
SELECT 1
FROM left_table lt
WHERE lt.member_id = 100
AND lt.group_id = rt.id
)
This is how my table looks like:
id | name | value
-----------------
1 | user1| 1
2 | user2| 1
3 | user3| 3
4 | user4| 8
5 | user5| 6
6 | user7| 4
7 | user8| 9
8 | user9| 2
What I want to do is to select all the other users, in one query, who's value is user1's value lower than it's value plus 3, higher than it's value minus 3 or equal to it's value.
Something like this:
$result = mysqli_query($con,"SELECT * FROM users WHERE value<'4' OR value>'-2'") or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo $row['name'].'<br/>';
}
The problem is that users1's value can vary every time the query is run.
Sorry for lame names, but this should work:
NOTE: I named table with your data as "st".
SELECT b.user, a.value as "user1val", b.value as "otheruservalue" FROM st as a
join st as b
on a.user = "user1" and a.user != b.user
where
(b.value > (a.value - 3)) and (b.value < (a.value + 3))
We get unique pairs of user1's value and other user's value by joining same table. After that we just do some simple comparison to filter rows with suitable values.
$user1 = mysql_fetch_assoc(mysql_query("SELECT `value` FROM `users` WHERE id='1'"));
$result = mysql_query("SELECT * FROM `users` WHERE value<'".$user1['value']."+3' OR value>'".$user1['value']."-3'");
Or nested queries :
$result = mysqli_query($con, "select * from `users` where `value` < (select `value` from `users` where `name`='user1')+3 OR `value` > (select `value` from `users` where `name`='user1')-3");
I have table:
user_id | song_id| points
--------|----------------
2 | 1 | 0
2 | 2 | 1
2 | 3 | 2
2 | 4 | 3
2 | 5 | 4
And I need to check if the user have changed the points value.
Therefore it should be something like:
while ($row = mysql_fetch_array($query)) {
$userID = $row['user_id'];
$songID = $row['song_id'];
$points = $row['points'];
if($songID-$points==1){
echo $userID."<br>";
}
But this will print out every occasion of userID where the song-id - points=1.
I need to print out only these user_id's that have all the values =1 and the username must echo'd only once.
EDIT:
SELECT DISTINCT user_id WHERE (song_id - points) = 1
This is half way there. This echo's user_ids' where the song_id - points = 1, but if the user is reordered (i use jQuery sortable) the list, then there can be some rows that is "song_id - points = 1".
My script must echo only these user_id-s, where users every song_id - points = 1, not only one
SELECT DISTINCT user_id FROM table WHERE (song_id - points) = 1
After edit:
SELECT table.user_id
FROM table
LEFT JOIN (
SELECT user_id, COUNT(*) AS C FROM table) AS T2
ON table.user_id = T2.user_id
WHERE (table.song_id - table.points) = 1
GROUP BY table.user_id
HAVING COUNT(*) = T2.C
You can first filter the users which has modified point values:
SELECT DISTINCT user_id FROM table
WHERE (song_id - points) != 1
Then you can use fetch the users which doesn't fit the above condition:
SELECT DISTINCT user_id FROM table
WHERE user_id NOT IN (
SELECT DISTINCT user_id FROM table
WHERE (song_id - points) != 1
)
According to your last edit this last SQL statement might work.
You can check a working example.
Here is what you're looking for:
select user_id from (
select user_id, if(song_id - points = 1, 0, 1) flag from t
) as S
group by user_id
having sum(flag) = 0
And here is a working example.
In case I didn't understand the requirements this shows all users who don't even have one row in which song_id - points != 1, i.e, all users who have all rows that match song_id - points = 1
Or maybe, if you prefer a different approach that might be more efficient:
select distinct t1.user_id from t t1
where not exists (
select * from t t2
where t2.song_id - t2.points != 1 and t1.user_id = t2.user_id
)
Here is the working example.
Not sure I understand the why of the situation, but a simple control-break structure will achieve the desired result ...
$old_id = '';
$good = false;
while($row = mysql_fetch_array($query)){
//check to see if we have a new user ...
if($row['user_id'] != $old_id){
//check to see if all values were == 1
if($good){
echo $old_id . '<br />';
}
//re-initialize variables
$good = true;
$old_id = $row['user_id'];
}
//if value != 1, we won't print the user ...
if($row['song_id'] - $row['points'] != 1){
$good = false;
}
}
//final end-of-loop condition ...
if($good){
echo $old_id . '<br />';
}
OK, here's a query that's a lot more simple than the join above:
SELECT user_id, sum(song_id) as song_total, sum(points) as point_total, count(*) AS cnt FROM table GROUP BY user_id
If the difference between song_id and points is 1 for every song, then the difference between the totals will equal the number of rows for that user ... so using this query:
while($row = mysql_fetch_array($query)){
if($row['cnt'] == ($row['song_total'] - $row['point_total'])){
echo $row['user_id'] . '<br />';
}
}