Need to use left join with limit but it works wrong - php

I have two sql query:
1)
SELECT DISTINCT item_id
FROM sh_itemviews WHERE login='".$login."'
ORDER by time DESC LIMIT 5"
2)
SELECT *
FROM sh_item WHERE id=itemIdFrom sh_itemviews "
So i want to do it better and i'm trying to use LEFT JOIN:
SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id;
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC LIMIT 5"
Like a result i have all data where sh_item.id = sh_itemviews.item_id; but other constructin WHERE is ignored.
What i'm doing wrong?
Thanks!

You have a semicolon just before the WHERE statement.
"SELECT DISTINCT * FROM sh_itemviews LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id**;** WHERE sh_itemviews.login='".$login."' ORDER by sh_itemviews.time DESC LIMIT 5"
The correct one is:
"SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC
LIMIT 5"

Extra semi column before the WHERE clause was the reason, Find the updated query below
SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC LIMIT 5"

You make incorrect syntax before where
Try this syntax
SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC
LIMIT 5"

Related

How i can use WHERE command when i already use join in my command

Code Below
<?php $sql = "SELECT tblstudents.FullName,tblbooks.BookName,tblbooks.ISBNNumber,tblissuedbookdetails.IssuesDate,tblissuedbookdetails.ReturnDate,tblissuedbookdetails.id as rid from tblissuedbookdetails join tblstudents on tblstudents.StudentId=tblissuedbookdetails.StudentId join tblbooks on tblbooks.id=tblissuedbookdetails.BookId order by tblissuedbookdetails.id desc";
$query = $dbh -> prepare($sql);
$query->execute();
I want to add WHERE so i can show only the specific data
I already try add WHERE in the end like this
<?php $sql = "SELECT tblstudents.FullName,tblbooks.BookName,tblbooks.ISBNNumber,tblissuedbookdetails.IssuesDate,tblissuedbookdetails.ReturnDate,tblissuedbookdetails.id as rid from tblissuedbookdetails join tblstudents on tblstudents.StudentId=tblissuedbookdetails.StudentId join tblbooks on tblbooks.id=tblissuedbookdetails.BookId order by tblissuedbookdetails.id desc WHERE RetrunedStatus = 1";
And this is my database
You're almost there. You got the order of the ORDER BY and WHERE clauses wrong. Move ORDER BY after your WHERE statement like this:
SELECT tblstudents.FullName,tblbooks.BookName,tblbooks.ISBNNumber,tblissuedbookdetails.IssuesDate,tblissuedbookdetails.ReturnDate,tblissuedbookdetails.id as rid from tblissuedbookdetails join tblstudents on tblstudents.StudentId=tblissuedbookdetails.StudentId join tblbooks on tblbooks.id=tblissuedbookdetails.BookId WHERE RetrunedStatus = 1 order by tblissuedbookdetails.id desc
From your schema I can see that your column name is RetrunStatus, where in your query you wrote 'RetrunedStatus'. These should be consistent, and may be renamed to 'ReturnStatus' to fix the typo.
I would suggest specifying which join you're willing to perform, also rearrange places of ORDER BY and WHERE clause and typo of RetrunStatus as mentioned by Ro.
SQL Example -
SELECT tblstudents.FullName,tblbooks.BookName,tblbooks.ISBNNumber,tblissuedbookdetails.IssuesDate,tblissuedbookdetails.ReturnDate,tblissuedbookdetails.id as rid from tblissuedbookdetails inner join tblstudents on tblstudents.StudentId=tblissuedbookdetails.StudentId inner join tblbooks on tblbooks.id=tblissuedbookdetails.BookId WHERE tblissuedbookdetails.RetrunStatus = 1 order by tblissuedbookdetails.id desc";

SELECT LEFT JOIN with LIMIT

I have a problem putting a limit on the number of rows from my Jokes table.
This is my query working, getting all rows:
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Would it be something like?
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM (
SELECT * FROM Jokes ORDER BY ID DESC Limit 0,40)
AS a
LEFT JOIN Categories
AS b
ON a.CategoryID = b.ID
why not using
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Limit 0,40

MySQL Output different values by the same id

I have a database containing two tables:
products_attributes ( id , image )
options_values ( id , text )
The id value is the same in both tables. Now I need to output the text and the image based on the same id from both tables.
Then possibly base this on the loop. I have created something like this, but it doesn't work correctly.
$sqlquery = "SELECT * FROM products_attributes JOIN options_values WHERE BY
id=id ASC LIMIT 0,40";
$sqlresult = #mysql_query($sqlquery);
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)) {
echo "<img src='$content[1]'/> $content[2]";
}
#EDIT
So the query ended up like this, however I can not get the attributes_image (table column name) to display. It does however work using the numerical system.
$sqlquery = "SELECT * FROM zen_products_attributes pa JOIN zen_products_options_values ov ON pa.options_values_id = ov.products_options_values_id LIMIT 0,40;";
$sqlresult = #mysql_query ($sqlquery);
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)){
echo $content['attributes_image'];
;}
Use
$sqlquery = "SELECT * FROM products_attributes JOIN options_values
ON products_attributes.id=options_values.id ASC LIMIT 0,40";
1:You need to use alias for tables when you are joining on same column
name to avoid ambiguity.i.e 'id=id' is wrong.
2.Syntax error.there is no 'WHERE BY' in mysql.
$sqlquery = "SELECT * FROM
products_attributes pa
JOIN options_values ov
on pa.id=ov.id ASC LIMIT 0,40";
Edit answer:
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)){
echo $content['attributes_image'];
;}
--^(extra semi colon,remove this)
if you have fields with same name give them alias.
SELECT p.`desc` as description, v.`desc` FROM products_attributes as p
JOIN options_values as v ON p.id=v.id
ASC LIMIT 0,40
SELECT
*
FROM
products_attributes pa
JOIN
options_values ov
ON
pa.id = ov.id
LIMIT
0,40;
SELECT * FROM products_attributes AS pa JOIN options_values AS ov ON pa.id=ov.id ORDER BY ov.id ASC LIMIT 0,40

SQL query GROUP BY id filter?

THIS IS THE OUTPUT OF THE QUERY:
SELECT * FROM
((SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
privatemsgs.relatedto
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='$myid'
AND relatedto=0 and bdel=1)
UNION
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
rel.relatedto
FROM privatemsgs AS rel
JOIN privatemsgs ON(rel.relatedto = privatemsgs.id)
LEFT JOIN
users AS u ON(rel.useraid = u.id)
WHERE rel.userbid='$myid'
)) privatemsgs
GROUP BY id
ORDER BY timee DESC
I got double id "2". first 1 with "isread = 0", second with "isread = 1".
When I added "group by id", I've got (line 2)
But i need the output to show that isread = 1 (like line 3)
How do i fix it?
add MAX(isread) near the *. GROUP BY works only on Aggregate functions

MySQL Query Not Working Properly

Where is the mistake?
Thank you!
SELECT Table.id,
Table.name,
Table.kommentar,
Table.pictureurl
WHERE Table.news_id = 5, //without this line the query works!
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS Table
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = Table.id
GROUP BY Table.id
ORDER BY Table.id DESC LIMIT 0,50
WHERE clause should be after the FROM clause
SELECT `Table`.id,
`Table`.name,
`Table`.kommentar,
`Table`.pictureurl,
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS `Table`
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = `Table`.id
WHERE `Table`.news_id = 5 // <=== HERE
GROUP BY `Table`.id
ORDER BY `Table`.id DESC LIMIT 0,50
One more thing, your alias which is Table should be escape with backtick since it a Reserved Keyword in MySQL
SELECT Table.id,
Table.name,
Table.kommentar,
Table.pictureurl,
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS Table
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = Table.id
WHERE Table.news_id = 5 //===> where should be here
GROUP BY Table.id
ORDER BY Table.id DESC LIMIT 0,50

Categories