$genre = 'Action|Adventure|Crime';
$starcast= 'Tom Hanks|Felicity Jones|Omar Sy|Irrfan Khan';
$S2 = mysql_query("SELECT title,hash,year FROM IMDB WHERE starcast REGEXP '".$starcast."' or genre REGEXP '".$genre."' LIMIT 16") or die (mysql_error());
while ($S = mysql_fetch_assoc($S2)){
this is working but results are in mixed data.
data LIMIT is 16.
i want 1st all data by starcast.
Example :- If startcast data match with 9 result so last 7 data needed match with genre.
sorry for bad English.
Question is UPDATE.....
I doubt how your posted query works. It should throw compilation error. Your query should look like
SELECT title,hash,year
FROM IMDB WHERE starcast REGEXP '".$starcast."' or genre REGEXP '".$genre."'
ORDER BY id DESC
LIMIT 16
If startcast data match with 9 result so last 7 data needed match with genre
don't think you can make it like that unless you use UNION query like
(SELECT title,hash,year
FROM IMDB WHERE starcast REGEXP '".$starcast."'
ORDER BY id DESC LIMIT 9)
UNION
(SELECT title,hash,year
FROM IMDB WHERE genre REGEXP '".$genre."'
ORDER BY id DESC LIMIT 7 )
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"
If there were a table like the follwing:
id title
1 a
2 b
3 c
4 d
I want to know if there is a way of selecting rows in a order by first selecting the rows with id less than 3 ordering them in id descending order, than selecting the rest in id descending order, so in this case, row 2,1,4,3.I was wondering if there was a sql statement something like this:
SELECT * FROM tablename ORDERY BY id<'3' id DESC, id DESC
You can use multiple expressions in the order by:
order by id < 3 desc,
id desc
This is close to your expression, but you have an extra id in it.
It will do the same like Gordon's answer. But I think this will make more sense for understanding.
ORDER BY CASE id < 3 THEN 1, ELSE 0 END DESC,
id DESC
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)
How can i found word in string using regex or not in this string
String is Mysql Query
SELECT
id,
name,
desc,
(SELECT id as lid FROM comments WHERE lid = id order by id desc limit 1) as lid_com
FROM posts limit 3
here i want to search for limit in the last of string
string may be
limit 3
limit 3, 3
limit 3 , 3
limit 3 ,3
3 here may be any number
i tried this regex but i'm beginner
"/ limit [0-9]{0,9}+\,[0-9]{0,9} /i$"
how can i do this
Thank you
$string = 'SELECT
id,
name,
desc,
(SELECT id as lid FROM comments WHERE lid = id order by id desc limit 1) as lid_com
FROM posts limit 3 ,3';
echo preg_match('/(limit)\s\d(((\,\s)|(\s\,\s)|(\s\,))\d)?$/i', $string, $matches); //1
print_r($matches); //$matches['0'] == 'limit 3 ,3'
"/limit[\s]+[\d]+[\s]*,[\s]*[\d]+$/i"
This searches case insesitive for:
"limit"
one or more whitespaces
one or more digits
zero or more whitespaces
a comma
zero or more whitespaces
one or more digits
at the end of the string
Do not remember, whether LIMIT is always a last SQL statement, so I'd use
/limit\s+(\d+)(?:\s*,\s*(\d+))?(?=[^\n]+\Z)/mi
Try this condition -
SELECT * FROM table WHERE column
REGEXP '(^limit[[:space:]]+[[:digit:]]+[[:space:]]*,[[:space:]]*[[:digit:]]+$)|(^limit[[:space:]]+[[:digit:]]+[[:space:]]*$)';
The first part find strings like 'limit 3,3' -
^limit[[:space:]]+[[:digit:]]+[[:space:]]*,[[:space:]]*[[:digit:]]+$
The second one finds strings like 'limit 3' -
^limit[[:space:]]+[[:digit:]]+[[:space:]]*$
I am coding a blog post kind of thing, where the author will post the article and it will be displayed in the front end, my problem starts for selecting the posts as i have to meet certain conditions for posting the news in the front end,
I have 4 fields in the database namely
title
pic_title
pic_brief
pic_detail
you guessed it right apart from the title table the rest of three will hold the path to the images in varchar datatype, which will be used to display as the post, the format of the front end is such that
a) there will be total of eight post
displaying in the front end (eight
entries from the database)
b) there will be three post on the top which will include the value from
the table title, pic_title and
pic_brief (total of 3 values)
c) and the rest five will contain just the title and pic_title
(excluding the three entries of top)
Please NOTE: i want the second query to exclude the top 3 record
which already exist in the top i.e
(first query = 3 post in descending
order, second query = 8 - first 3 = 5
post)
The Order of the Post i want is by id DESC
EDIT: I took the first query as
SELECT * FROM news ORDER BY id DESC LIMIT 3
Now if i take the same second query and try populating the values by desc order again the same records will be accessed
In simple words i want a query that will skip the last three records order by id DESC
How do i achieve this feat in PHP?
If you just want the SQL, here it is:
First query
SELECT * FROM `table` LIMIT 3
Second query
SELECT * FROM `table` LIMIT 3,5
(where table is the name of your table of course. Of course you may want to add some ORDER BY clause. To execute these queries in PHP, I suggest reading the manual. If you have any specific problems after doing so, then you can post a new question.
This is a situation where I'd likely opt to select all eight records at once - the less trips to the database, the better.
SELECT t.title,
t.pic_title,
t.pic_brief
FROM TABLE t
ORDER BY t.id DESC
LIMIT 8
...because the rest is just presentation:
$query = sprintf("SELECT t.title,
t.pic_title,
t.pic_brief
FROM TABLE t
ORDER BY t.id DESC
LIMIT 8");
// Perform Query
$result = mysql_query($query) or die( mysql_error() );
$rowcount = 1;
// Use result
while ($row = mysql_fetch_assoc($result)) {
if(rowcount <= 3) {
echo $row['title']
echo $row['pic_title']
echo $row['pic_brief']
} else {
echo $row['title']
echo $row['pic_title']
}
++$rowcount;
}
first query will be like this
"select title, pic_title , pic_brief from table_name order by post_id desc limit 0 , 3"
and rest of five will be
"select title, pic_title from table_name order by post_id desc limit 3 , 5"
second query will exclude the three results returned by first query...
If you want more perfection you can collect all three Ids returned by first query and can add NOT IN in second query.
"select title, pic_title from table_name where post_id not in (1,2,3) order by post_id desc limit 0 , 5";