for exam I have 2 table
1) Post - and its columns :
pid :
uid :
title :
content :
created date :
...
2) URL alias
aid :
sou_url :
des_url :
The second table used to store url-alias for all pages in my site
I created an function to get alias like this
function get_alias_url($surl);
for exam get_alias_url("pid/50") = "this-is-my-first-post";
So now, I want to select all posts that has url alias's length > 50
I wondering,is there any way to make a query to do that?
It may be look like :
Select * from post where length(get_alias_url("pid/nid")) > 50
Thanks
LENGTH() returns the length of the string measured in bytes.
CHAR_LENGTH() returns the length of the string measured in characters.
$query = 'SELECT * FROM post WHERE char_length('. get_alias_url("pid/nid").' ) > 50';
Your PHP function should still work inside your query.
Hope this helps!
You can use the MySQL CHAR_LENGTH() function in your query.
For a full list of functions related to strings in MySQL, go here
Related
Hello i'm new in mysql and i have to run a multiple update on my table.
I have 700 records in the table and i have to update them all this way:
table example :
store_id: 1
store_email: storename#gmail.com
for single update i use
UPDATE stores SET email = '1#gmail.com' WHERE id = 1;
i need to update all the emails and replace their name with their id, so it would be like this:
storename#gmail.com --> 1#gmail.com
storename#gmail.com --> 2#gmail.com
storename#gmail.com --> 3#gmail.com
those numers have to be the ID for each store.
Hope you can understand
Thanks for help.
P.S. i need to run it on magento 2
you can use CONCAT() and RIGHT() function for manipulating strings like this:
UPDATE stores SET email = CONCAT(id, RIGHT(email, 9));
The RIGHT('string', n) function extracts n characters (storemail = 9 chars in your case) from a string (starting from right).
Since you are adding id to String column gmail, you can use contact() fucntion like below :
UPDATE stors SET email=CONCAT(id, "#gmail.com") where id=2;
I need one help. I need to fetch value from table as per multiple ids which are in comma separated string using MySQL. I am explaining my table and query below.
db_basic:
id special name
1 2,3,4,5 Raj
2 4,2,5,6 Rahul
3 3,5,6 Rocky
My code is given below.
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special='".$special."'");
Here I need where special=2 is present inside that comma separated string those value will be fetched. I need only proper query. Please help me.
use below query
May be work for u
Use LIKE instead of "="
"select * from db_basic where special LIKE '%".$special."%'"
use FIND_IN_SET function for ex.
select * from db_basic where FIND_IN_SET(2,special)
Try this once,
select * from db_basic where FIND_IN_SET($special, special);
It should work.
if you current code expeted to return the first and second line of your db, you can use the LIKE operator in the query like this :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special LIKE '%,".$special.",%'");
or LOCATE like :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where LOCATE(".$special.",special)>0");
And FIND_IN_SET : From the docco here - "This function does not work properly if the first argument contains a comma (“,”) character".
I'm working on a project using the pages in php / mysql and html; I have a table that contains the data for calls made from a PBX and save the number called, the source, date, time, etc ... what I want to do is to search within this table all the phone numbers that have the first 4 digits equal to those that pass through the query, only that i have no idea how to pull off only the 4-digit or at least how to make a control character by character of the value contained in the field. I tell you now that the field is a varchar. Thank you in advance :)
To do that in MySQL query, either
SELECT *
FROM <tablename>
WHERE LEFT(<column>, 4) = "<4 digits>"
or
SELECT *
FROM <tablename>
WHERE <column> LIKE "<4 digits>%"
or in the PHP side :
if (strpos($column,'<4 digit>') !== false) {
echo 'true';
}
Use this, to get substring
SELECT aut_name,
RIGHT(aut_name,7)
FROM author
WHERE country='UK';
See more at: http://www.w3resource.com/mysql/string-functions/mysql-right-function.php#sthash.xKNwZeki.dpuf
I suggest this solution:
$variableWhereYoustoreTheFourDigits="1234"; //Use whatever you have in your code to set the value.
$result =$mysqli->query("SELECT number FROM yourtable where number LIKE \"$variableWhereYoustoreTheFourDigits%\");
I am want to write a php mysql query which includes and and range condition.
In the screen shot you can see the field of the table called search. The fields with same name are the range. I want select query and it should include all the fields and their appropriate range.
The names of the fields are shape1,shape2(it is range from shape1 to shape2) etc and it goes on.
The query should be like this
select * from search where unique_id='$unique_id && (carat1='$carat1' between carat2='carat2') &&...
and there are other field too like cut and shape, all in one query. I am inserting value in to the database directly from android in json format. My problem is that i don't know proper format.
Please help me
It should go like this (an example)
select * from search
where unique_id= 10
and carat1 between 1 and 10
and shape1 between 1 and 10
and cut1 between 1 and 10
EDIT:
If you are trying to run the SQL from PHP script then the format will be different like below (if the column is type integer like unique_id then don't put ' while replacing value. For string type replace with ' like carat)
Select * from search
where unique_id = $unique_id
and carat between '$carat1' and '$carat2'
and color between '$color1' and '$color2'
and shape between '$shape1' and '$shape2'
Example usage of between in mysql:
SELECT * FROM search
WHERE unique_id = 200 AND
carat BETWEEN 1 AND 5;
Note that the AND that follows BETWEEN is used to indicate the range (1,5) and is not a logical AND operator.
Try this, I hope this will help you
SELECT * FROM search
WHERE unique_id = $unique_id AND
carat between $carat1 AND $carat2 AND
color like '%".$color1."%' AND
color like '%".$color2."%' AND
shape like '%".$shape1."%' AND
shape like '%".$shape2."%'
So, i have this database right, with some fields called 'id', 'title' and 'message'. Now i got like 700 messages in the database. So all i wanna do, is set a limit of max 50 message title's per page, and make multiple pages... How can i do that?
I only know to get the first page, using LIMIT...
As you guessed, you have to use the LIMIT keyword.
It accepts two value (quoting) :
the offset of the first row to return
the maximum number of rows to return
In your case, you'll have to use something like this for the first page :
select * from your_table order by ... limit 0, 50
And, then, for the second page :
select * from your_table order by ... limit 50, 50
And for the third one :
select * from your_table order by ... limit 100, 50
And so on ;-)
Edit after the comment : to get the page number, you'll have to receive it from your URLs, that would look like this :
http://www.example.com/page.php?pagenum=2
Then, you'll calculate the first value for the limit, :
$offset = 50 * intval($_GET['pagenum']);
And inject it in your query :
select * from your_table order by ... limit $offset, 50
Constructing URLs to the differents pages is now a matter of getting URLs such as these :
http://www.example.com/page.php?pagenum=0
http://www.example.com/page.php?pagenum=1
http://www.example.com/page.php?pagenum=2
...
If you know you have 700 elements, and 50 per page, you'll have 700/50 pages ;-)
So, something like this should do the trick :
for ($i=0 ; $i<700/50 ; i++) {
// Use http://www.example.com/page.php?pagenum=$i as URL
}
Of course, 700 is a value that can probably change, and should not be hard-coded : it should be determined from the database, using a count query :
select count(*) as total
from your_table
...
Your PHP file may receive a GET argument being the page number.
Then you do your query with LIMIT ($page_number * $messages_per_page), $messages_per_page (pseudo-code).
$messages_per_page = 50 in your case. $page_number is deduced from a GET argument, after sanitizing, the first page being page number 0.