So i'm trying to get a row of msg's of friends, but what i exactly got is multiple rows of one msg. So i did some research but i couldn't find anything about it only the opposite ways to do it.
Now i have a second problem after a user send me this query:
SELECT msg, userpost_ID, created, userone_ID, usertwo_ID, accepted
FROM friendship, Posts
WHERE friendship.userone_ID = '13' AND (Posts.userpost_ID = friendship.usertwo_ID OR Posts.userpost_ID = '13')
Now i have used the JOIN syntax but it displays the same problem here the query:
SELECT P.msg, P.userpost_ID, P.created,
F.userone_ID, F.usertwo_ID, F.accepted, U.fname, U.lname FROM Posts P JOIN friendship F ON F.userone_ID = '13' JOIN Users U ON U.ID = F.usertwo_ID
You have to add braces around the OR
SELECT
msg,
userpost_ID,
created,
userone_ID,
usertwo_ID,
accepted
FROM
friendship,
Posts
WHERE
friendship.userone_ID = '13'
AND (Posts.userpost_ID = friendship.usertwo_ID
OR Posts.userpost_ID = '13')
Also use modern explizite join not implizit by using where
You have had mistake in query just use below query to get accurate results OR simply add brackets in OR condition
SELECT
msg,
userpost_ID,
created,
userone_ID,
usertwo_ID,
accepted
FROM
friendship,
Posts
WHERE
friendship.userone_ID = '13'
AND (Posts.userpost_ID = friendship.usertwo_ID
OR Posts.userpost_ID = '13')
Hope this will help you!!
Thanks & Regards
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. That would pretty much fix your problem:
SELECT . . .
FROM friendship f JOIN
Posts p
ON p.userpost_ID = f.usertwo_ID
WHERE f.userone_ID = 13;
Notes:
You only need p.userpost_ID = 13 if you want the user's messages as well.
Table aliases make the query easier to write and to read.
You should qualify all column names (specify the table where they come from).
Related
Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.
I have two tables:
sk_accounts //details of user
acnt_user_id
acnt_fname //first name
acnt_lname
acnt_profile_picture
acnt_member_class
so on........
sk_following //table containing details of users who are following others
id
flwing_follower_id //id of the user who are followed by other followers
flwing_following_user_id
following_date
I want to display details of the follower based on the following Mysql code.Unfortunately it returns zero rows eventhough there are 3 rows.My query is like this:
$query_following = "SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture
FROM sk_following
INNER JOIN sk_accounts
WHERE sk_following.flwing_follower_id='$id' AND sk_accounts.acnt_user_id=sk_following.flwing_following_user_id AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'";
$result_following = mysql_query($query_following);
$count_following = mysql_num_rows($result_following);
echo $count_following;
Note:$id and $name contain values
Kindly help me.Thanks in advance.
Try this,
"SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture
FROM sk_following
LEFT JOIN sk_accounts ON sk_accounts.acnt_user_id=sk_following.flwing_following_user_id
WHERE sk_following.flwing_follower_id='$id'
AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'";
may this help you.
Hard to completely understand without seeing sample data and desired output, but should your JOIN be on the flwing_follower_id and not the flwing_following_user_id?
SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture
FROM sk_following
INNER JOIN sk_accounts ON sk_accounts.acnt_user_id=sk_following.flwing_follower_id
WHERE sk_following.flwing_follower_id='$id'
AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'
Good luck.
I have two tables:
task
id name dueDate completed projectID
project
id name dueDate completed
I need to query both tables for rows with the same data. I tried doing a sample statement just looking for rows with completed=0, but never got any results. I think it has to do with using OR instead of AND, but it's just a little above my level right now...Any ideas?
TO CLARIFY, I'm not looking for duplicate rows, I'm looking for 'all tasks and projects with completed = 0'
The query is:
SELECT * FROM "task" t, "project" p WHERE t.dueDate = "2012-08-17" OR p.dueDate = "2012-08-17" AND t.completed = 0 OR p.completed = 0
I did manage to get one of the answers' code to work, however I realized that my entire app was written to talk to one table, and that it would be much easier to just combine the task and project table and use an isProject column to differentiate projects from tasks. This also adds the ability to nest projects inside of projects, because projects will now have a projectID column as well.
In the end, KISS prevails...
Thanks for all the help! I will mark the answer that worked, even though I won't be using it.
Try using parenthesis.
SELECT * FROM "task" t, "project" p WHERE (t.dueDate = "2012-08-17" OR p.dueDate = "2012-08-17") AND (t.completed = 0 OR p.completed = 0)
If You want only values matches from both tables with completed=0 from dueDate='2012-08-17':
You can use join to bound that tables results into one.
Inner join will return only results which matches on both sides.
So You can use it to match them in both tables by it and then filter for Your wanted value by classic where:
select * from task t inner join project p on t.dueDate = p.dueDate and t.completed = p.completed
where t.dueDate = '2012-08-17' and t.completed = 0
Try this instead:
SELECT dueDate, completed
FROM task AS t
WHERE (dueDate = "2012-08-17" AND completed = 0)
UNION ALL
SELECT dueDate, completed
FROM project AS p
WHERE (dueDate = "2012-08-17" AND completed = 0)
This should give you all records from each table where dueDate = "2012-08-17" and completed = 0.
I am not sure if that join statement is writen in the right way:
<?php
function generateComment($commentID)
{
$avatar_Q=mysql_query("
SELECT * FROM comments com
INNER JOIN users us ON com.user_id=us.user_id
WHERE comment_id=$commentID // will that $commentID be red in that query string or will it treat it as a string "commentID"
");
if($row=mysql_fetch_array($avatar_Q))
{
$userName=$row["us.user_name"]; // do I refer to the fields like that
$avatarPath=$row["us.avatar"];
$avatarRep=$row["us.reputation"];
$message=$row["com.comment"];
$date=$row["com.date"];
}
mysql_close();
if(!isset($avatarPath))
{
$avatarPath="blank picture";
}
?>
Is this the most efficient way to write a join statement
Your query is written correctly, but you can improve it by specifing the table on comment_id, and for a better returning I suggest you to specify with column you want back, also I will use the variable outside the "string", because it's a numeric value (I suppose you omit the single quote like...
$sql = "Select com.command_it, etc
FROM ..... WHERE com.comment_id = ".$commentID."";
If otherwise the com.comment_id is text or varchar you must use the single quote like:
$sql = "Select com.command_it, etc
FROM ..... WHERE com.comment_id = '".$commentID."'";
Moreover this way you get only the rows where there is a user and a comment, if one user have no comment you don't retrieve that user...
If you want the user also if he has no comments you must use a LEFT JOIN
$sql = "SELECT com.comment_id, etc FROM users us
LEFT JOIN comments com ON com.user_id=us.user_id
WHERE com.comment_id=".$commentID."";
Not sure what you're doing with this line
WHERE comment_id=$commentID // will that $commentID be red in that query string or will it treat it as a string "commentID"
");
But you need to specify which table comment_id is coming from, say comments, then you might do something like
$avatar_Q=mysql_query("SELECT * FROM comments com
INNER JOIN users us ON com.user_id=us.user_id
WHERE com.comment_id=$commentID");
Not sur if it's the best way, but you could try to mysqlslap a mysql DB with it. Compare it with left join and other types of join and see which one is the best in your case. MySQL is all about slapping.
I have an issue getting data from three tables, which I want to return using one query.
I've done this before using a query something like this one:
$query = mysql_query("SELECT
maintable.`id`,
maintable.`somedata`,
maintable.`subtable1_id`,
subtable1.`somedata`,
subtable1.`subtable2_id`,
subtable2.`somedata`
FROM
`maintable` maintable,
`subtable1` subtable1,
`subtable2` subtable2
WHERE
maintable.`somedata` = 'some_search' AND
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
")or die(mysql_error());
The problem this time is that the extra details might not actually apply. I need to return all results that match some_search in maintable, even if there is no subtable1_id specified.
I need something that will go along the lines of
WHERE
maintable.`somedata` = 'some_search'
AND IF maintable.`subtable1_id` IS NOT NULL (
WHERE subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
)
As you will probably guess, I am not an advanced mysql user! Try as I might, I cannot get the syntax right, and I have had no luck searching for solutions on the web.
Any help much appreciated!
It seems like the basic distinction you're looking for is between an INNER JOIN and a LEFT JOIN in MySQL.
An INNER JOIN will require a reference between the two tables. There must be a match on both sides for the row to be returned.
A LEFT JOIN will return matches in both rows, like an INNER, but it will also returns rows from your primary table even if no rows match in your secondary tables -- their fields will be NULL.
You can find example syntax in the docs.
If I got this right, you need to use MySQL LEFT JOIN. Try this:
SELECT
m.id,
m.somedata,
m.subtable1_id,
s1.somedata,
s1.subtable2_id,
s2.somedata
FROM
maintable m
LEFT JOIN
subtable1 s1
ON
m.subtable1_id = s1.id
LEFT JOIN
subtable2 s2
ON
s1.subtable2_id = s2.id
WHERE
m.somedata = 'some search'
This will return the data of maintable even if there's no equivalent data in subtable1 or 2 OR data of maintable and subtable1 if there's no equivalent data in subtable2.
How about this:
WHERE
maintable.`somedata` = 'some_search'
AND (maintable.`subtable1_id` IS NULL OR (
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id` )
)
Keep in mind that this will result in a cross product of subtable1 and subtable2 with maintable when subtable1_id is NULL.