to search the data from database - sql

I want to search the data from database from table. Throw one input box I give nput (like mobile, name, id, anything I want ) and search and give out put

Logic is something like that
SELECT * FROM table WHERE (condition)
For your case, you may as well use
SELECT 'mobile','name','id', '...any from its table...' FROM table_name

Related

Why do CONTAINS and LIKE return different results?

I have the following query. There are two possible columns that may hold the value I'm looking for, let's call them FieldA and FieldB.
If I execute this:
SELECT COUNT(1)
FROM Table
WHERE CONTAINS(Table.*, 'string')
I get back "0".
However, if I execute this:
SELECT COUNT(1)
FROM TABLE
WHERE FieldA LIKE '%string%' OR FieldB LIKE '%string%'
I get back something like 9000. I then checked and there are rows that have the word string in either FieldA.
Why does this happen? I recall that CONTAINS uses a full-text index, but I also recall that LIKE does the same, so if the problem was that the indexes are outdated, then it should fail for both of them, right?
Thanks
I believe that CONTAINS and full text searching will only yield whole word results, so you won't match the same as LIKE '%string%'. If you want to right wildcard your CONTAINS, you must write it like:
SELECT COUNT(1) FROM Table WHERE CONTAINS(Table.*, '"string*"')
However, if you want to left wildcard, you can't! You have to store a copy of your database reversed and then do:
SELECT COUNT(1) FROM Table WHERE CONTAINS(Table.*, '"gnirts*"')
https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms552152(v=office.14)
How do you get leading wildcard full-text searches to work in SQL Server?
So in the example in the question, doing a CONTAINS(Table.*, 'string') is not the same as doing LIKE '%string%' and would not have the same results.

Ignore a SELECT LIKE statement if an identical one has already been satisfied.

I have a table with 4 entries.
CREATE TABLE tab(
name Text
);
INSERT INTO "tab" VALUES('Intertek');
INSERT INTO "tab" VALUES('Pntertek');
INSERT INTO "tab" VALUES('Ontertek');
INSERT INTO "tab" VALUES('ZTPay');
Pntertek & Ontertek are fuzzy duplicates of the correctly spelt Intertek. I wish to create a list consisting of fuzzy duplicates and the correctly spelt names.
As I have 4 names, I have 4 search criteria:
SELECT name FROM tab WHERE name LIKE '%ntertek'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%ntertek') >1;
SELECT name FROM tab WHERE name LIKE '%ntertek'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%ntertek') >1;
SELECT name FROM tab WHERE name LIKE '%ntertek'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%ntertek') >1;
SELECT name FROM tab WHERE name LIKE '%TPay'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%TPay') >1;
This produces 3 lists containing the same information. I would like to ignore the 2nd and 3rd identical SELECT statements if the first one returns a result. Is this possible using SQLite and how would I do this?
I'm very much a beginner when it comes to sqlite and programming in general so any help would be greatly appreciated.
Thanks in advance.
What do you want the query to return? Just potential duplicates? If so you could do the above with one query by including a having statement. However, the method that you are using at the moment only allows for differences at the start of the name. I would suggest looking into something like an edit-distance algorithm (sometimes referred to as Levenshtein distance) to identify the number of characters you would need to change on one field to make it the same as another.
There are details of a possible SQLite implementation in the following link: http://www.sqlite.org/spellfix1.html

How to Unselect The Field in select Query using sql

hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.

how to display a particular field first and remaining next in MYSQL

I have a table structure like :
ID, Test_ID, Verdict, PATH, Last_Status,
Present_status, Remote_location,TestCase
I want the result to be displayed starting with TestCase
But I do not want to mention all the fileds particularly.
Is there anything like
select TestCase,* from Table order by TestCase` ?
Basically the result should be displayed as
Testcase, ID, Test_ID, Verdictm PATH,
Last_Status, Present_statusmRemote_location
If I try the above select, it does not work in MYSQL. Is there any command to achive what I require?
Thanks.
Not that I know of. Even so, it's good practice to avoid using SELECT * FROM tableName wherever possible.
If your column list ever changes, queries in your code may assign values to the wrong fields.
you have to list all the fields you want in the order you want them in. it is either use * or list what you want, and just for the future using * is bad practice.
yes, just list the columns you want first explicitly in the Select clause:
Select Testcase, t.* From TableName
But the asterisk will cause the testcase column to be output no matter what, so you will get it twice. To avoid ambiguous redundant column names, you will have to alias the first one:
Select Testcase as FirsttestCase, t.* From TableName

How to implement a Keyword Search in MySQL?

I am new to SQL programming.
I have a table job where the fields are id, position, category, location, salary range, description, refno.
I want to implement a keyword search from the front end. The keyword can reside in any of the fields of the above table.
This is the query I have tried but it consist of so many duplicate rows:
SELECT
a.*,
b.catname
FROM
job a,
category b
WHERE
a.catid = b.catid AND
a.jobsalrange = '15001-20000' AND
a.jobloc = 'Berkshire' AND
a.jobpos LIKE '%sales%' OR
a.jobloc LIKE '%sales%' OR
a.jobsal LIKE '%sales%' OR
a.jobref LIKE '%sales%' OR
a.jobemail LIKE '%sales%' OR
a.jobsalrange LIKE '%sales%' OR
b.catname LIKE '%sales%'
For a single keyword on VARCHAR fields you can use LIKE:
SELECT id, category, location
FROM table
WHERE
(
category LIKE '%keyword%'
OR location LIKE '%keyword%'
)
For a description you're usually better adding a full text index and doing a Full-Text Search (MyISAM only):
SELECT id, description
FROM table
WHERE MATCH (description) AGAINST('keyword1 keyword2')
SELECT
*
FROM
yourtable
WHERE
id LIKE '%keyword%'
OR position LIKE '%keyword%'
OR category LIKE '%keyword%'
OR location LIKE '%keyword%'
OR description LIKE '%keyword%'
OR refno LIKE '%keyword%';
Ideally, have a keyword table containing the fields:
Keyword
Id
Count (possibly)
with an index on Keyword. Create an insert/update/delete trigger on the other table so that, when a row is changed, every keyword is extracted and put into (or replaced in) this table.
You'll also need a table of words to not count as keywords (if, and, so, but, ...).
In this way, you'll get the best speed for queries wanting to look for the keywords and you can implement (relatively easily) more complex queries such as "contains Java and RCA1802".
"LIKE" queries will work but they won't scale as well.
Personally, I wouldn't use the LIKE string comparison on the ID field or any other numeric field. It doesn't make sense for a search for ID# "216" to return 16216, 21651, 3216087, 5321668..., and so on and so forth; likewise with salary.
Also, if you want to use prepared statements to prevent SQL injections, you would use a query string like:
SELECT * FROM job WHERE `position` LIKE CONCAT('%', ? ,'%') OR ...
I will explain the method i usally prefer:
First of all you need to take into consideration that for this method you will sacrifice memory with the aim of gaining computation speed.
Second you need to have a the right to edit the table structure.
1) Add a field (i usually call it "digest") where you store all the data from the table.
The field will look like:
"n-n1-n2-n3-n4-n5-n6-n7-n8-n9" etc.. where n is a single word
I achieve this using a regular expression thar replaces " " with "-".
This field is the result of all the table data "digested" in one sigle string.
2) Use the LIKE statement %keyword% on the digest field:
SELECT * FROM table WHERE digest LIKE %keyword%
you can even build a qUery with a little loop so you can search for multiple keywords at the same time looking like:
SELECT * FROM table WHERE
digest LIKE %keyword1% AND
digest LIKE %keyword2% AND
digest LIKE %keyword3% ...
You can find another simpler option in a thread here: Match Against.. with a more detail help in 11.9.2. Boolean Full-Text Searches
This is just in case someone need a more compact option. This will require to create an Index FULLTEXT in the table, which can be accomplish easily.
Information on how to create Indexes (MySQL): MySQL FULLTEXT Indexing and Searching
In the FULLTEXT Index you can have more than one column listed, the result would be an SQL Statement with an index named search:
SELECT *,MATCH (`column`) AGAINST('+keyword1* +keyword2* +keyword3*') as relevance FROM `documents`USE INDEX(search) WHERE MATCH (`column`) AGAINST('+keyword1* +keyword2* +keyword3*' IN BOOLEAN MODE) ORDER BY relevance;
I tried with multiple columns, with no luck. Even though multiple columns are allowed in indexes, you still need an index for each column to use with Match/Against Statement.
Depending in your criterias you can use either options.
I know this is a bit late but what I did to our application is this. Hope this will help someone tho. But it works for me:
SELECT * FROM `landmarks` WHERE `landmark_name` OR `landmark_description` OR `landmark_address` LIKE '%keyword'
OR `landmark_name` OR `landmark_description` OR `landmark_address` LIKE 'keyword%'
OR `landmark_name` OR `landmark_description` OR `landmark_address` LIKE '%keyword%'