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.
Related
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).
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.
So i fetch my data from two tables in my php and encode it in one json object. I got everything i needed except that it doubles the display. And my teamone is located in the matches tables. instead of starting from array 0, it starts after the schedules tables. Which is array 7. I dont know why this happen.
Here is my php.
$sql = "SELECT * from schedule, matches;";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("start"=>$row[4],"end"=>$row[5],"venue"=>$row[6], "teamone"=>$row[8], "teamtwo"=>$row[9],
"s_name"=>$row[17]));
}
echo json_encode (array("schedule_response"=>$response));
mysqli_close($con);
?>
Here is my display. As you can see there are four displays but in my database it only has 2. It doubles the display. How do i fix this? Thanks
{ "schedule_response":[
{"start":"2016-11-10 00:00:00","end":"2016-11-04 00:00:00","venue":"bbbb","teamone":"aaa","teamtwo":"bbb",
"s_name":"hehehe"},
{"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbbbbbb","teamone":"aaa","teamtwo":"bbb",
"s_name":"hehehe"},
{"start":"2016-11-10 00:00:00","end":"2016-11-04 00:00:00","venue":"bbbb","teamone":"ehe","teamtwo":"aha",
"s_name":"aaa"},
{"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbbbbbb","teamone":"ehe","teamtwo":"aha",
"s_name":"aaa"}]}
I need to get the teamone, teamtwo and s_name values from the matches while i need the start, end and the venue from the schedule table in one query.
Schedule table
Matches Table
Because of your SQL query, you should not forget to perform some form of a grouping (the way you select results from both table defines that):
$sql = "SELECT * from schedule s, matches m GROUP BY s.id"; //I assume that your table schedule has an `id`
Or, you can rework the query to be more readable:
$sql = "SELECT s.*, m.* FROM schedule s
INNER JOIN matches m ON m.schedule_id = s.id
GROUP BY s.id"; //I assume that you have such database design that you have defined foreign key on table `matches`.
Of course, the INNER JOIN above could be LEFT OUTER JOIN - it all depends on your database design.
I think it is the problem with mysqli_fetch_array().
Can you please change mysqli_fetch_array() to mysqli_fetch_assoc()
I have two databases and I am trying to compare two tables. My code does not seem to be working, not sure what I am doing wrong.
Here is the code.
<?php
include 'connection.php';
/*
* This code compares between two tables
*/
//SQL call
$getData = $connection->prepare("SELECT `CustomerCity` FROM `auth` LEFT JOIN `tb_data.cobs.city` WHERE `CustomerCity` = `tb_data.cobs.city` LIMIT 3");
$getData->execute();
$gotData = $getData->fetchAll(PDO::FETCH_ASSOC);
print_r($gotData);
In my database I have two tables, on is cobs, the other is tb_data. tb_data has a table called cobs and auth is a table within a database called d_data. Both of these tables have a city column. I need get every record in auth that has a city that matches in the cobs table.
That looks like the query is using a mixture of explicit join syntax with obsolescent syntax for the join using the WHERE clause for the join conditions.
If so try:-
SELECT CustomerCity
FROM auth
LEFT JOIN tb_data.cobs
ON auth.CustomerCity = cobs.city
LIMIT 3
Others have pointed out that your query is wrong, but have not provided a correct answer. This is what you are likely looking for:
SELECT `auth.CustomerCity` FROM `auth`
LEFT JOIN `tb_data.cobs` ON `tb_data.cobs.city` = `auth.CustomerCity`
LIMIT 3
Try this :
Select *.auth, *.cobs from auth join cobs on auth.city = cobs.city limit 3
The query is incorrect, you need to specify the link betweeen the tables auth and tb_data.cobs.city. For example:
SELECT
*
FROM
FOOTABLE FOO
LEFT JOIN
BARTABLE BAR ON BAR.FOO_ID FOO.ID = -- here goes the link between them
WHERE
...
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.