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%'
Related
I'm looking for a way to search a table values from the results of a query on another table :
SELECT entry_id FROM FEEDENTRYSTATUSES WHERE starred > 0 ;
+----------+
| entry_id |
+----------+
| 1036 |
| 1059 |
+----------+
2 rows in set (0.00 sec)
SELECT url from FEEDENTRIES WHERE id = 1036 ;
+---------------------+
| url |
+---------------------+
| https://google.com/ |
+---------------------+
1 row in set (0.00 sec)
So I can get a list of IDs from the FEEDENTRYSTATUSES table.
I can retrieve value from second table manually by giving the value 1036.
But I would like to search in table FEEDENTRIES from values returned by first SELECT.
Is here a way to do so ?
Any help very much appreciated.
Thanks a lot
You can use IN operator and subquery to achieve that:
SELECT url from FEEDENTRIES
WHERE id IN (SELECT entry_id FROM FEEDENTRYSTATUSES WHERE starred > 0);
Or join the tables and filter the data that you need:
SELECT fe.url from FEEDENTRIES fe
INNER JOIN FEEDENTRYSTATUSES fes ON fe.id = fes.entry_id
WHERE fes.starred > 0;
I need to do an update query making an md5 hash from my google calendar column. This is my query:
UPDATE `ea_appointments` SET `hash` = MD5(`id_google_calendar`)
Would this work to make something like this?:
Table: ea_appointments
id_google_calendar Hash
e5e3were760lkj792c7t5vm61bvk_20160729T200000Z d5f9f4ef02e438d49c8bf39cd4b4118d
Yes, it'll be work.
And you can loosely check it by:
select md5('test');
Result:
+----------------------------------+
| md5('test') |
+----------------------------------+
| 098f6bcd4621d373cade4e832627b4f6 |
+----------------------------------+
Or:
select md5('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z');
Result:
+------------------------------------------------------+
| md5('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z') |
+------------------------------------------------------+
| 06b5a13d9a7b0ed26ab1406434954972 |
+------------------------------------------------------+
Or:
create table t(id_google_calendar varchar(100), hash varchar(100));
insert into t values ('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z', '');
update t set Hash = md5(id_google_calendar);
select * from t;
Result:
+-----------------------------------------------+----------------------------------+
| id_google_calendar | hash |
+-----------------------------------------------+----------------------------------+
| e5e3were760lkj792c7t5vm61bvk_20160729T200000Z | 06b5a13d9a7b0ed26ab1406434954972 |
+-----------------------------------------------+----------------------------------+
I think it would work.
Also I suggest that you test it first.
You can test query with:
Select statement:
SELECT id_google_calendar, MD5(id_google_calendar) as hash FROM ea_appointments
Update record in test table. Create some test table add few record in it and run query
I have one search box that I would like to have search 6 columns in my schools database when an input is made. So far the search box searches the name field only and returns a match for exact or partial inputs.
I would like to search for a specific city and have all results show up from the name AND city columns (instead of just the name column) and so on.
Say I wanted to search with a zip code, I would like the listings to be all schools in that zip code. And finally if I input 2 words (e.g. penn Philadelphia) I would like all penn schools to show that are in the name column AND city column only. (not just all the penns in the name or every school in Philadelphia) and so on. These may be an elementary questions on the matter but I've been searching for days with no success. Maybe better use of wildcards and the "AND" "OR" clauses would benefit me.
Can someone help me structure a sql query to accomplish this?
this is what I have so far:
SELECT * FROM schools
WHERE (name='%name%' OR address='%name%' OR city='%name%'OR zip='%name%')
There are few ways to do that-
The very basic strategy is to match input with our table columns by the query like as you mentioned -
1. SELECT * FROM table WHERE (name='%name%' or zip='%name%' or city='%name%');
2. SELECT * FROM table WHERE LOCATE(name, GROUP_CONCAT(name,city,zip)) > 0;
3.
SELECT * FROM table WHERE name like '%name%'
UNION
SELECT * FROM table WHERE name like '%name%'
UNION
SELECT * FROM table WHERE name like '%name%';
but suppose the case where input box have the string- "varun bharti" but actual name in database is "varun bal bharti" So when you search you will missed the record. for that case you should break the string by space in to array elements and use these queries for elements or either you can replace the space in name column and match.
set #var=REPLACE ('varun bharti', ' ', '%');
SELECT * FROM table WHERE name like concat('%',#var,'%') or
zip like concat('%',#var,'%') or
city like concat('%',#var,'%');
You can also use regualar expressions for that.
For example input string the day boss get
Hitesh> select * from test;
+--------------------+
| name |
+--------------------+
| i am the boss |
| You will get soon |
| Happy birthday bro |
| the beautiful girl |
| oyee its sunday |
+--------------------+
5 rows in set (0.00 sec)
Hitesh> set #var=CONCAT('.*',REPLACE('the day boss get',' ','.*|.*'),'.*');
Query OK, 0 rows affected (0.00 sec)
Hitesh> select #var;
+----------------------------------+
| #var |
+----------------------------------+
| .*the.*|.*day.*|.*boss.*|.*get.* |
+----------------------------------+
1 row in set (0.00 sec)
Hitesh> select * from test where name REGEXP #var;
+--------------------+
| name |
+--------------------+
| i am the boss |
| You will get soon |
| Happy birthday bro |
| the beautiful girl |
| oyee its sunday |
+--------------------+
5 rows in set (0.00 sec)
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])$';
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).