I have a problem with sql query in php:
select
user, name, outlet, switch, port, vlan, mac, status
from access where
user like '%'
and name like '%'
and outlet like '%'
and switch like '%'
and port like '%'
and vlan like '%'
and mac like '%'
and status like '%'
order by 'user';
When running query on MySQL client version: 5.1.36 query doesn't work totally (ORDER BY won't work), however when running SAME query on MySQL client version: 4.1.13, ORDER BY works!
I have checked nearly all manuals about ORDER BY, WHERE, LIKE commands, but no result. No mention about version differences, etc..
You have to remove the quotes from user in the ORDER BY clause. This is what is causing the ORDER BY not working as expected, because you can use any expression in the ORDER BY clause, and the 'user' in quotes is being considered an expression (constant) instead of a column name.
Test case (MySQL 5.1.45):
CREATE TABLE tb (id int);
INSERT INTO tb VALUES (5);
INSERT INTO tb VALUES (1);
INSERT INTO tb VALUES (4);
INSERT INTO tb VALUES (2);
INSERT INTO tb VALUES (3);
SELECT * FROM tb ORDER BY 'id';
+------+
| id |
+------+
| 5 |
| 1 |
| 4 |
| 2 |
| 3 |
+------+
5 rows in set (0.00 sec)
SELECT * FROM tb ORDER BY id;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)
I think what you need is:
SELECT `user`,`name`,`outlet`,`switch`,`port`,`vlan`,`mac`,`status`
FROM `access`
WHERE `user` like '%'
AND `name` like '%'
AND `outlet` like '%'
AND `switch` like '%'
AND `port` like '%'
AND `vlan` like '%'
AND `mac` like '%'
AND `status` like '%'
ORDER BY `user`;
Though I don't understand your WHERE clause. It doesn't filter on any of the fields.
EDIT; some of your column names (user, name, port and status) could be MySQL keywords. Try enclosing them in grave accents (`) (I added them to my post as well).
Related
I have a query like so:
SELECT * FROM `mytable` WHERE `code` LIKE 'PR-SM' AND number = '45'
However in some instances the number can be PR45 which will then fail. I have tried the following:
SELECT * FROM `mytable` WHERE `code` LIKE 'PR-SM' AND CAST(number as unsigned) = '45'
SELECT * FROM `mytable` WHERE `code` LIKE 'PR-SM' AND CONVERT(number as unsigned) = '45'
SELECT * FROM `mytable` WHERE `code` LIKE 'PR-SM' AND number LIKE '%45'
LIKE % will not work as it also picks up 145.
REGEXP_REPLACE also does not work as I am on MySQL 5.7
This is needed for my API, because of my setup I need to be able to do it in the where clause.
Sample Table:
+--------------------------+
| code | number |
+--------------------------+
| PR-SM | PR45 |
+--------------------------+
| PR-SM | PR145 |
+--------------------------+
| XYZ | 177 |
+--------------------------+
| XYZ | 81 |
+--------------------------+
Although you are not using MySQL 8+, which means you won't have access to the newer regex functions, on 5.7 REGEXP should still be available:
SELECT *
FROM yourTable
WHERE code = 'PR-SM' AND number REGEXP '(^|[^0-9])45([^0-9]|$)';
Demo
The regex pattern used here says to match:
(^|[^0-9]) match the start of the input OR a non digit
45 match the number 45
([^0-9]|$) match the end of the input OR a non digit
I have a table that contain list of book. I want to count the number of row with "mysql php" in it.
If I use regular like (use the code below), it only count the row with 'mysql php'.
SELECT 'mysql php' as searched_word,
count(case when book_list like '%mysql php%' then 1 else 0 end) AS number_of_occurrences
FROM book_list
What I want is, it also count the row with "php mysql" value, but I don't have idea how to do it.
This is the table:
+------------------------+
| book_list |
+------------------------+
| mysql php for dummies |
+------------------------+
| learn php mysql |
+------------------------+
| mysql php for students |
+------------------------+
| mysql database |
+------------------------+
my expected result:
+---------------+-----------------------+
| searched_word | number_of_occurrences |
+---------------+-----------------------+
| mysql php | 3 |
+---------------+-----------------------+
Make the condition an or, and use sum() (not count()):
select
'mysql php' as searched_word,
sum(book_list like '%mysql php%' or book_list like '%php mysql%') AS number_of_occurrences
from book_list
Note the way MySQL allows briefer code to count conditions, because 'true' is '1' and 'false' is '0'.
If you wanted to count rows that had both terms somewhere, eg "learn php and mysql", use this:
sum(book_list like '%php%' and book_list like '%mysql%')
WITH
-- parse searching criteria to separate tokens (delimiter - space), remove duplicates
cte1 AS (
SELECT DISTINCT token
FROM JSON_TABLE( CONCAT('["', REPLACE(#criteria, ' ', '","'), '"]'),
"$[*]" COLUMNS( token VARCHAR(254) PATH "$" )
) AS jsontable
),
-- select books which' title contains ALL tokens as complete words
-- words delimiter - space
-- commas, dots and another punctuation is NOT removed
cte2 AS (
SELECT book_list.book_list
FROM book_list
JOIN cte1 ON LOCATE(cte1.token, REPLACE(book_list.book_list, ' ', CHAR(0)))
GROUP BY book_list.book_list
HAVING COUNT(*) = ( SELECT COUNT(*)
FROM cte1 )
)
-- count the amount of matched titles
SELECT #criteria searched_word, COUNT(*) number_of_occurrences
FROM cte2;
fiddle
PS. Needed MySQL 8.0.4 or newer.
I have mysql table with multiple columns, a column has string data type namely code and the values of the column might be digits only or mixed of string and digits e.g: one row value has 57677 and other rows column value like 2cskjf893 or 78732sdjfh.
mysql> select * from testuser;
+------+--------------+
| id | code |
+------+--------------+
| 1 | 232 |
| 2 | adfksa121dfk |
| 3 | 12sdf |
| 4 | dasd231 |
| 5 | 897 |
+------+--------------+
5 rows in set (0.00 sec)
mysql> show create table testuser;
+----------+------------------------------------------------------------------ ---------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------ ---------------------------------------------------------------+
| testuser | CREATE TABLE `testuser` (
`id` int(11) DEFAULT NULL,
`code` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------ ---------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
I would like to filter out only rows which has numeric value in code column or filter rows which has string values in code column.
try this
Mysql Query to fetch only integer values from a particular column
SELECT column FROM TABLE where column NOT REGEXP '^[0-9]+$' ;
Mysql Query to fetch only string values from a particular column
SELECT column FROM TABLE where column REGEXP '^[0-9]+$' ;
To get digits
SELECT id,code
FROM table_name
WHERE code REGEXP '^[0-9]+$';
To get string values
SELECT *
FROM table_name
WHERE code REGEXP '^[a-zA-Z]+$'
Use regular expressions
SELECT *
FROM myTable
WHERE code REGEXP '^[0-9]+$';
This will filter out column values with all digits.
You can use regular expressions in mysql too.
SELECT * FROM `table` WHERE `code` REGEXP '[0-9]';
Results will be the rows, where any number in the code column.
Try this,
select id,code from table_name where code NOT REGEXP '^[0-9]+$'
Or,
select id,code from table_name where code like '%[^0-9]%'
Database changed
mysql> select * from userinfo;
+-----------+----------+-----------------------+------------+
| firstname | lastname | username | password |
+-----------+----------+-----------------------+------------+
| asif | kolu | ashufound | 123456 |
| faisal | samad | tfhgfhgfh#gmail.com | 123456 |
| kamran | shafat | kamthemaam | kamoos |
| ubaid | mir | sadfsfsff#yahoo.com | qwertasd |
| majid | mir | zsffsa | afdfdsf |
+-----------+----------+-----------------------+------------+
5 rows in set (0.00 sec)
mysql> SELECT * from userinfo WHERE lastname = 'mir';
Empty set (0.10 sec)
mysql> SELECT * from userinfo WHERE lastname='mir';
Empty set (0.00 sec)
what is wrong with this code simple where clause not working?actually problem is in the code for insert i think
your last name in your table may have a space before or after mir
mir
^---^---look and remove spaces from here in your table
I m usign select Query in this the where clause doesnt working query is
select * from table_t where id = '96'
this query is resulting 0 rows but
when i try
select * from table_t where id like '96'
this query is working fine.
and when i try like with column name like
select id from table_t where id like '96'
returning 0 rows
the id is auto generated primary key not have white spaces
why????
is there any database issue???
this query is working fine on my local machine but when i try it online it is misbehaving.
Thanx.
1- You may have space before or after "mir".
2- You may have special (invisible) characters before or after 'mir' or even between its characters.
To solve this problem, I suggest to do this first:
Update userinfo
set lastname = 'mir'
where (username = 'sadfsfsff#yahoo.com') or (username = 'zsffsa')
And then, run this to check:
Select * from userinfo where lastname = 'mir'
I see some good answers but in the case that you couldn't update leading or trailing spaces in your entire db for each name, you could write the select a little differently. If it is a space issue, try this.
SELECT * from userinfo WHERE TRIM(lastname) = TRIM('mir')
If that doesn't work, try LIKE and see if you get results. That could help with debugging.
SELECT * FROM userinfo WHERE lastname LIKE '%mir%'
For example, I am having a column storing data like this.
Apple
12.5.126.40
Smite
Abby
127.0.0.1
56.5.4.8
9876543210
Notes
How to select out only the rows with data in IP format?
I have tried with '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
but I have no idea why it also matches 9876543210
You're going to need to use REGEXP to match the IP address dotted quad pattern.
SELECT *
FROM yourtable
WHERE
thecolumn REGEXP '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$'
Technically, this will match values that are not valid IP addresses, like 999.999.999.999, but that may not be important. What is important, is fixing your data such that IP addresses are stored in their own column separate from whatever other data you have in here. It is almost always a bad idea to mix data types in one column.
mysql> SELECT '9876543210' REGEXP '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$';
+---------------------------------------------------------------------------+
| '9876543210' REGEXP '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$' |
+---------------------------------------------------------------------------+
| 0 |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT '987.654.321.0' REGEXP '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$';
+------------------------------------------------------------------------------+
| '987.654.321.0' REGEXP '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$' |
+------------------------------------------------------------------------------+
| 1 |
+------------------------------------------------------------------------------+
Another method is to attempt to convert the IP address to a long integer via MySQL's INET_ATON() function. An invalid address will return NULL.
This method is likely to be more efficient than the regular expression.
You may embed it in a WHERE condition like: WHERE INET_ATON(thecolumn) IS NOT NULL
SELECT INET_ATON('127.0.0.1');
+------------------------+
| INET_ATON('127.0.0.1') |
+------------------------+
| 2130706433 |
+------------------------+
SELECT INET_ATON('notes');
+--------------------+
| INET_ATON('notes') |
+--------------------+
| NULL |
+--------------------+
SELECT INET_ATON('56.99.9999.44');
+----------------------------+
| INET_ATON('56.99.9999.44') |
+----------------------------+
| NULL |
+----------------------------+
IS_IPV4() is a native mysql function that lets you check whether a value is a valid IP Version 4.
SELECT *
FROM ip_containing_table
WHERE IS_IPV4(ip_containing_column);
I don't have data, but I reckon that this must be the most solid and efficient way to do this.
There are also similar native functions that check for IP Version 6 etc.
This may not be the most efficient way, and it's not technically regex, but it should work:
SELECT col1 FROM t1 WHERE col1 LIKE '%.%.%.%';
you could also use the useful function inet_aton()
SELECT *
FROM yourtable
WHERE inet_aton(thecolumn) is not null
Lengthy but works fine:
mysql> SELECT '1.0.0.127' regexp '^([0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.([0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.([0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.([0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])$';