I done my project in Yii. Then i wants to display the values related items which i showing.
i wanna display related recipes like cuisine and course field from the recipe table.
i show my condition here. how to write the query for to display the two field based on the related items.
$query="SELECT * FROM recipe where cuisinename, course_id LIKE '%$cuisine%' AND course_id LIKE '%$course%' ORDER BY recipe_id DESC LIMIT 4";
then i tried this code also:
$query="SELECT * FROM recipe where type LIKE '%$type%' ORDER BY recipe_id DESC LIMIT 4";
its working but i wants to display both field values to be related.
Try this
$query="SELECT * FROM recipe where cuisinename LIKE '%$cuisine%' AND course_id LIKE '%$course%' ORDER BY recipe_id DESC LIMIT 4";
Your first query syntax is not correct, if you want to use 2 conditions as you are trying to apply in your first query then use below query:
SELECT * FROM recipe WHERE cuisinename LIKE '%$cuisine%' AND course_id LIKE '%$course%' ORDER BY recipe_id DESC LIMIT 4;
But if you want to add 3rd condition also which is in your 2nd query then use below query:
SELECT * FROM recipe WHERE cuisinename LIKE '%$cuisine%' AND course_id LIKE '%$course%' AND `type` LIKE '%$type%' ORDER BY recipe_id DESC LIMIT 4;
If you want something else then please elaborate your question.
Related
Now I use this code:
$query = mysqli_query($conn, "(SELECT * FROM movies WHERE
MATCH(title) AGAINST('".$_SESSION['mtitle']."' WITH QUERY EXPANSION) AND
id NOT LIKE '".$_SESSION['mid']."')
UNION
(SELECT * FROM movies WHERE category LIKE '%".$cat."%' AND
id NOT LIKE '".$_SESSION['mid']."' ORDER BY RAND()) LIMIT 5");
I would like to list 5 similar movies. When there is 5 or more matches in the first part, it works good. But if isn't, the second selection isn't random, it writes always the same movies in the same sequence.
For example: If I search for Jumanji, the first suggestion is Jumanji, the second is Jumanji: Welcome to the Jungle. But the last 3 is always the same movie.
Group By think be your answer to remove any duplicate values.
`GROUP BY mtitle`
"(SELECT mtitle FROM movies WHERE
MATCH(title) AGAINST('".$_SESSION['mtitle']."' WITH QUERY EXPANSION) AND
id NOT LIKE '".$_SESSION['mid']."')
UNION
(SELECT mtitle FROM movies WHERE category LIKE '%".$cat."%' AND
id NOT LIKE '".$_SESSION['mid']."') GROUP BY mtitle ORDER BY RAND() LIMIT 5"
I have 2 tables, names and phones I did this for the query
$result = mysqli_query($mysqli, "SELECT * FROM names ORDER BY fname ASC
RIGHT JOIN phones ON phones.id=names.phone_id"
);
I got $result as false. My names table has a column named phone_id and it's a PK of phones's id, like so
names
- phone_id (FK)
phones
- id (PK)
What's wrong with my sql above?
The syntax should go like this:
SELECT *
FROM names
RIGHT JOIN phones ON phones.id = names.phone_id
ORDER BY fname ASC
The ORDER BY had to be moved to the end.
Order by should be the last part of your query
SELECT *
FROM names n
RIGHT JOIN phones p ON p.id=n.phone_id
ORDER BY fname ASC
Start using alias names to make the query more readable
You should use ORDER BY as last clause of your query. Becasue order by i.e. sorting works at last after fetch.
SELECT *
FROM names as n
RIGHT JOIN phones as p ON p.id=n.phone_id
ORDER BY fname ASC
To see more abour ORDER BY you can check the manual link
First I need to get exact match like
SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC
and then append to these results query with LIKE match
SELECT * FROM movies WHERE title LIKE '%STRING&' AND title<>'STRING' ORDER BY x DESC
and limit these results with maximum of 10 results.
UNION wont`t do the jobs as it sorts all results together and returns wrong order (I need exact match first, then with LIKE)
SELECT * FROM movies WHERE title='STRING' UNION
SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10
The best solution I got is to use multi_query()
$query = "SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC; ";
$query .= "SELECT * FROM movies WHERE title LIKE '%STRING%' AND title<>'red' ORDER BY x DESC";
$Dbi->multi_query($query);
do {
$sql = $Dbi->store_result();
while($x = $sql->fetch_array()) {
...
}
} while($Dbi->next_result());
but in this case it is not possible to use any mysql inside the inner loop and there also must be better looking solution!
You can do this with one query, by using the order by clause:
SELECT *
FROM movies
WHERE title like '%STRING%'
ORDER BY title = 'STRING' desc,
title like '%STRING%' desc
LIMIT 10;
The first clause in the ORDER BY puts the exact matches first. The second then orders by the partial matches. The WHERE clause ensures that there is a match of some kind.
You don't need the UNION, it's accessing the same table twice:
SELECT *
FROM movies
WHERE title LIKE '%STRING&'
ORDER BY CASE WHEN title='STRING' THEN 1 ELSE 2 END
LIMIT 10
(SELECT * FROM movies WHERE title='STRING')
UNION
(SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10)
Hi i am trying to get distinct values and some other data from same table A . the code i tried is
$query2="select DISTINCT(from_id) from messages where to_id='$userid' order by messagedate DESC";
$res2=mysql_query($query2);
while($row2=mysql_fetch_row($res2))
{
$query="select * from messages where to_id='$userid' and from_id='$row2[0]' ORDER BY messagedate DESC"
using the above method i am unable to filter distinct values hence i tried like this
select msgid,DISTINCT(from_id) from messages where to_id='21' order by messagedate DESC
Its an error. need help pls
Try like this
select DISTINCT(from_id),msgid from messages where to_id='21' order by from_id DESC
Look into the GROUP BY statement
I think you need something like
SELECT msgid, from_id
FROM messages
WHERE to_id = 21
GROUP BY from_id
ORDER BY messagedata DESC
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
I am using this to get customer name for auto complete function.
$query = $db->query("SELECT orderr_customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%' GROUP by orderr_customer_name LIMIT 10");
if($query) {
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.$result->orderr_customer_name.'\');">'.$result->orderr_customer_name.'</li>';
How can i Select another table? this is the table i want to use
("SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 10");
Thanks alot.
You should look into something like that (Note that I can't test it now, this is only a guideline):
SELECT orderr.orderr_customer_name, customer.customer_name
FROM customer INNER JOIN orderr_customer_name ON orderr.orderr_customer_name = customer.customer_name
WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 1
Pleasy read the doc: Here
It sounds like you're asking how to query and combine the results from two tables through the use of a single query. This can be accomplished rather easily with a UNION in MySQL.
(SELECT orderr_customer_name AS customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%')
UNION
(SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%')
ORDER BY customer_name LIMIT 10
Don't forget to make sure you escape/sanitize $queryString, and that you have proper indexes on your tables. Also, in your specific example, it appears you might be duplicating customer names in at least one place; while some applications require this, you may also want to consider normalization - but that's another topic.