Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Search Through Rows Until You Hit Field With Right Value


Basically, I'm trying to figure out how i can scroll through the rows in a mysql table until I hit a row which has a field containing a certain string. This is a ruff image of how my database looks (ignore the dots...it wouldn't let me leave white space):

id | username | some_field | | some_other_field |

1..|...bill........|...............| |........................|
2..|...fred......|...a value..| |........a value.......|
3..|...ted.......|...............| |........................|
4..|...jeff.......|...............| |........a value......|
5..|...craig.....|...............| |........................|

So, basically, i need to be able to start the query a Fred's 'some_field' then I need it to search on until it finds the next field with 'a value' in...in this case it would be Fred's 'some_other_field', but it could also be any other field in the table.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
One Field With Multiple Attached Rows, Only Show The Field Once?
I have a table called "Customers", with a field called "Customer name" and a "Customer ID". I have another table called "Projects", with a field called "Project name" and a field called "Customer ID" which references to "Customers"

What i am trying to do is: Select a customer. Select the projects that are related to the customer by comparing the Customer ID field. Code:

Full Text Search And 50% Rows
If a full text search returns more the 50% rows, it will return empty sets.

My database is relatively small.

1) How could I know that it returns the empty sets because it has got more than 50% rows, so I can inform the user that he is using a "too common" words, instead of leaving him sitting there wondering why he cannot find anything with a very popular search keyword?

2) Is there anyway I can change the rule, say if it returns 80% or 90% rows, then make it empty sets? Or turn off this 50% rules totally? Just return whatever they find?

Quickly Search Full Rows
I'm building a search thingy and some of my SQL looks like this right now:
Code:

SELECT title,description,link FROM table WHERE title LIKE '%foo%' OR description LIKE '%foo%' or link LIKE '%foo%'

I am thinking it might be faster to use a join or a view or something to search a whole row, but I am fairly new to SQL. How to quickly search full rows?

Optimizing Search Query For Millions Of Rows
I have mysql 4.1 and Im having a difficult time optimizing this query.

select domain, length(domain) as len
from domains
where
length(domain) <= &#3916;' and
not (domain regexp '[[:digit:]]') and
domain not like '%-%' and
price > &#390;' and price < &#3916;' and
end > &#55614;&#57159;-12-01'
order by end ASC, len ASC
The following query outputs:


| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
-------------------------------------------------------------------------------------------------------------------------------------
| 1 | SIMPLE | domains | ALL | end | NULL | NULL | NULL | 2600000 | Extra where; Using filesort |
My indexes are:
ID - PRIMARY, Unique
domain - Unique
end

Is there anyway this query could be optimized anymore? With only 2.6 million rows its taking a 5 or 6 seconds. It looks like its not finding the right keys.

Getting A Pic To Come Up From With A Search Field
ive got a news table in my database. its got a field where i input a word such as car name e.g ford!

now i also have a pictures table where i store the name of the picture e.g ford1.jpeg

what i want to do is basically bring the news on my page up and also the picture next to it! BUT! i want to do get the data in my search field and then search my pictures table for matching data! so if i have ford in my search field i want ford1.jpeg to come up! does that make sense? i want a ford picture to come up even though the name of the file is ford1.jpg.

News table:

header
body
search_word

pictures:
file_id
file_name

Search One Field
i have a table with username and list as fields
the list field contains a string that contains a lists of items, each one on a seperate line.is there any easier way to search the whole databases for a phrase conatined within the list field and have returned just the complete line where the phrase is and the username.
without having to load each one and do a search on each entry

Search Within A Field (mysql)?
I would like to make a search within a field in a database. For example, if I have a library of books and I want to search for all the titles of the books that have the "php" keyword in the title field. Something like:

select title from library where title="...php..."

Any idea of how to do this query??

How To Search A Value In Any Field Of Table??
I need to find a value in a table, the table i create have 26 field ( named as R1 ~ R26), but i don't know the value is in which filed, normally a database should have a reference field, so when we need to search for some value, we just simply type "SELECT * FROM table WHERE field like '%value%' ". But in this case, i dnt have a reference field, what command i can use to solve this problem??

Search In A Text Field
I have a text field that holds a description. For example we will say the description is for a tree so it would be "A tall plant with a bushy green top." Is there anyway for when a user searches for "Tall Plant" that "tree" will be returned? I can not think of a select statement that would accomplish this, is there any kind of function such as isIn() or something?

Search A Date Field
I have a date field defined as:

'created_date' timestamp NOT NULL default '0000-00-00 00:00:00'

It is also an indexed KEY. What would be the fastest search on that field if my desired results were for records created during a specific month?

created_date LIKE '2007-07%'

created_date REGEXP '^2007-07'

created_date>'2007-07-01 00:00:00' AND created_date<'2007-08-01 00:00:00'

Thanks for your opinions.

BTW, if you know Perl do you think it would be faster for MySQL to give me created _date data as DAY(created_date) or for Perl to split the date (first on a space character and again on the hyphen character)?

MySQL Field Search
I need a way to search a field for a keyword.

The field can have values such as "1 shadow", "4 shadow", "shadow 4/5", "joined g2", "skilled g5", and other similar values.

I need something that would return all instances of a keyword.
Ex, search for "shadow" would return "1 shadow", "4 shadow", "shadow 4/5"

Search From Beginning Of Word In Field.
I have "name" field with the values : "apple", "banana", and "apple and banana".

I want to query that strings and find the string "an".

If i query `name` LIKE '%an%' it will return allso the "banana" - i want just "and"

It is a problem for me because I want to search the words in the "name" field.
The query should search from the beginning of each word. haw can i do this ?

How Do I Search A Mysql Text Field?
I am looking to create a query that will search a mysql text field. I have tried something like this:

select field from table where textfield like '%Term%'

However, it does not work how I would like it to. Say for example I have the following text in my "textfield", how would I search the text so that it would return the row if it found the "test" word:

hello, my name is bradley and this is just a quick test to see if everything is working okay......

Search A Field For Different Words, Not As A Phrase
I wish to search a field for multiple words but not as a phrase, like i have it work at the moment. say one of my entries is...

'once upon a time'

i wish to search for a combination of words say 'time once' is my search query how do i change my query to get these results too.

my query to find just the phrase is..

$query = "SELECT * FROM `list` WHERE `datafield` LIKE '%phrase to find%'";

Text Field Search/replace
I need to do a mass search & replace on a TEXT column in a db. All I need to do is replace a few characters, but there's over 500 affected rows. Is there a way to do this in MySQL? I've created some html forms to do the work manually, if I have to.

Simple All Words Search Of A Field?
i've been using:

"...WHERE name LIKE %".$q."%"

to phrase search my SQL database, but I need to do an all keywords style search instead...

what's the simplest implementation of an "all keywords" search using string '$q' in field 'title' in table 'music' ?

Search And Replace On Char Field
I am trying to do a search and replace on a char(11) field. My guess is that the following SQL will replace all occurences of the string:

UPDATE table_name SET table_field=REPLACE(table_field,'string_to_find','string_to_replace');

However, I only want to replace the string if it occurs at the beginning of the field. I would guess I need to use LIKE 'string_to_find%' somehow, but I'm not sure on how to combine this with the above statement.

Using Value Input Field In Search Code
My code for searching my table is:

// read data from database
$result = mysql_query("select * from $table WHERE categories LIKE '%club%' order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

What I want to do is get the value of an input box into the srtring search %club%, so instead of searching for club it searches for what the user put into the input box.

My html code is this:

<form id="frmSearch" name="frmSearch" method="post" action="">
<label>
<input name="inputSearch" type="text" id="inputSearch" size="20" maxlength="12" />
</label>
<label>
<input type="submit" name="Submit" value="Search" />
</label>
</form>

Everything works fine but I'd be over the moon if you could help me to use the users input and search for that.

Update One Field In All Rows
I have a mysql database which contains 24,000 rows. I added a field called "RECALL", which contain null values. What I'm trying to do is fill that field in all 24,000 rows with the value "N".

Alter One Field In A Row (300 Rows)
example changing id='1' -> id=' 5000'

this must be done with over 300 ids

Is there a script ot there that can do this? instead of me doing it manually.


Concat A Field In Up To 4 Rows?
A customer can have between 1 and 4 rows, is it possible to make a query that will merge field "A" of all rows and then discard all other rows leaving only 1 row left with the merged data in field "A".

Count Same Field In Rows
i need to calculate the number of the same IP address that appear in the database, how can i do that?
example of the database:
IP Address Date and Time
127.0.0.1 5/23/2007
127.0.0.1 5/24/2007
192.168.1.2 5/25/2007
192.168.1.5 5/26/2007
127.0.0.1 5/27/2007

SELECTing Rows Where 1 Field Doesn't Repeat
Let's say I have a table something like this:


id a b c
1 1 2 3
2 1 3 4
3 2 1 1
4 2 8 4
5 3 1 4
6 3 8 1

And then I want to select the rows, where "a" field doesn't repeat, so the 1st, 3rd and 5th. How do I do that?

Not Selecting Rows Where A Specified Field Is Empty
What I am trying to do in a query, is only select rows where a certain field is not empty. For example, I have a visitor logs table, with the following fields:

id | date | pageVisited | referrer

Now, I only want to select the rows where there is a referrer. Something like:

Concatenating Multiple Rows To One Field?
I got a slight problem with a web application, its supposed to several
entries for a certain ID, and display them as one string.

Example:

ID | Name
-------------
1 | Jack
1 | Peter
2 | John
2 | Mark
2 | Ellis

Expected Result for ID 1 should be "Jack, Peter" for example, or "John,
Mark, Ellis" for 2.

Least problem would be clipping off a trainling or leading ",", but the
main problem is a SELECT statement to get them all in one go. Else I'd
have to select every Name per ID, and concatenate them in PHP or
elsewhere. Given its around 100 IDs, I'd have to do like 100 querys to
get the names, *in addition* to the Querys I need before and after.

I want to avoid that, but I couldnt find a simple solution that works
in MySQL 4.1 and above, aswell as it mustn't involve Stored Functions
or something, because I got no access to the mysqld itself, to add the
funcs as modules. Code:

Deleting Multiple Rows Have The Same Field Value
I have 100 rows that all have the field vaule of 'XYZ' I would like to
delete only 10 of those rows.

Is there such a query like "Delete 10 rows from table where table.field
= 'XYZ'"

Select Rows Where A Field Value Exists In Another Table
I have a table with a column of email addresses I have a second table with a column of email addresses I want to select all the rows in the first table whose email value exists in the second table Can't get a query to do this without an error,

Clearing Values From All Rows Inside A Certain Field
How could I clear all the rows value in a specific field? Here's an example:

How would I clear math and english from field: FAV SUBJECT?

table1

NAME|AGE|CLASSROOM|FAV SUBJECT
John|12|3B|math
Stacy|11|3B|english

Limit Number Of Rows Or Characters Returned In A Text Field
I understand you can limit the rows of a recordset, but can you somehow tell a query to limit the rows (or characters) returned within just a text field?

I have a page that returns abstracts of many articles and I want to truncate the description for each to just 200 characters. Is there a way to accomplish this with MySQL?

Updating Rows In Table B Based On Related Field In Table A
Ver 4.1.8-standard for apple-darwin7.6.0 on powerpc (Official MySQL-standard binary)

I am trying to do some data migration based. I have several tables that contain our legacy pkey field and I want to update the tables with new ID's.
I need to do this several times and have tried it several ways to no avail.

Table A
---------
companyID int(10) pKey
legacyID int(10) old legacy pkey

Table B
---------
bAID int(10) pkey
companyID int(10)
legacyID int(10)

Table A has values for both companyID (unique key) and legacyID.
Table B has values for bAID (unique key) and legacyID but companyID is empty.

I need to update tableB.companyID with tableA.companyID based on tableb.cSerialID to tablea.cSerialID relationship.

I need a query that will update ALL rows.

Search: Remove Quotes From Database For Search
I am making a book site and currently in the database are book titles likes "John's Trip" and "Peter's new bike". I have a search function where the customer can type in "Peter's" and it will bring up everything with "Peter's" in it.

But i was wonderind is there any way i can search for "Peters" and "Johns".

Searching For Rows That Depend On Other Rows In Some Fashion...
Maybe this is a very stupid question. I'll try anayway. I'm using
MySQL 3.23.52 on RedHat 8.0.

I have a very big table (several millions of rows). Each entry
actually constitute a word in a big text. Among other fields we have
an ID for each word which represents the postition in the text. Now I
want to search for short phrases.

For example "welcome to sweden". This means i want to find all
occurences of the word "welcome" with ID x, where we have the word
"to" with ID x+1 and the word "sweden" with ID x+2. I've tried doing
this recursively with temporary tables - but a find myself hitting a
wall as i'm not allowed to refer to 2 different temporary tables in
the same query...

Resorting to another type of data strucutre, full text index etc.. is
unfortunately not an option. We only need this as an add on feature if
we can do it. We believe that indexes, data structures etc.. are near
optimal as is.

Please use my email for further conversion since I'm not a frequent
usenet reader.

Zero Rows Or One Rows Returned, Same Data And Same Query
I have a query that produces a single row (as I expect) when I run it from the mysql client (mysql 4.0.18-Max/linux, also 5.0.19-standard/OSX-intel), or from sqlgrinder (osx, uses jdbc).

When I run it inside my application (a Java app connecting via jdbc), I get zero rows from this query.

I tried it under phpmyadmin, and once again I get zero rows.

Why do I get inconsistent results? Here's the query:

Insert New Rows With Qty Values From Existing Rows
I am trying to make all my products unique in my db.
Lets say I have a row with an id, it also has all relevant details about the product and it has a quantity value of 4.
What I would like to do is take the entire row and duplicate it by the number of the quantity field.
This would result in the same product 4 times instead of one product with qty of 4.
Reason, I am going to serialise all the products so that they each have unique barcode from the id field.
I will encounter the reverse issue when running an insert statement for inputting new data, i.e. insert a new row for each item depending on its qty value??

Searching For Rows That Depend On Other Rows In Some Fashion...
Maybe this is a very stupid question. I'll try anayway. I'm using
MySQL 3.23.52 on RedHat 8.0.

I have a very big table (several millions of rows). Each entry
actually constitute a word in a big text. Among other fields we have
an ID for each word which represents the postition in the text. Now I
want to search for short phrases.

For example "welcome to sweden". This means i want to find all
occurences of the word "welcome" with ID x, where we have the word
"to" with ID x+1 and the word "sweden" with ID x+2. I've tried doing
this recursively with temporary tables - but a find myself hitting a
wall as i'm not allowed to refer to 2 different temporary tables in
the same query...

Resorting to another type of data strucutre, full text index etc.. is
unfortunately not an option. We only need this as an add on feature if
we can do it. We believe that indexes, data structures etc.. are near
optimal as is.

Getting Rows That Are Related To Other Rows In The Same Table
I use a table to save a map using the following structure:

id, x, y, owner

Every occupied map filed has an owner id != 0. The owner id is = 0 for vacant fields.

now the problem:

New registered users need a vacant field on the map. Moreover the mapfields around this field need to be vacant as well! (sqrt((t1.x-t2.x)*(t1.x-t2.x)+(t1.y-t2.y)*(t1.y-t2.y)) <= 5.25)

What I need is a query that gets those fields that have vacant fields around them.

So far, all my tries to solve this problem with Joins/Suvqueries failed.

I Have A Search Form To Search Through The DB.
I have a DB with 50,000 entries. I have a search form to search through the DB. If the recno=30123456 and you search the recno, you get that one file. what if i want a query that searches alll recno's that contain 30?? Can I do that using mysql or do I need to program that in my java app?

Retrieve Field Value When Submitting To Table Auto Increment Primary Field Value
In my form, I write to four fields in the table, (fields 2,3 4 and 5), which creates a new record. the first field of this new record is an auto-incremental field, giving a unique id.

How can I write to the table and at the same time retrieve that unique id accurately, even if more than one person is performing write tasks simultaneaously?

My first thought is that if I grab the previous records id and add 1, then this may not match my newly inputted record, given that it may take a few minutes to add the record and someone else could have been there before me with a different record. (I hope that makes sense).

Then I thought that on opening the form, it could write to a new field inputting my loginID. Then I can retrieve the unique auto-increment number before then filling out my form and over-writing the loginID that I had previously entered. Sounds too convoluted to be the best way.

How To Convert A Varchar Field Into Proper Mysql Date Field?
I have a database in which the date is stored in varchar field in a following format: d-m-Y (06-08-2007), now the problem is that I want to change that field into mySQL date field as well as convert my older dates into MySQL date format i-e Y-m-d (2007-08-06)..

There are about 300 old entries..is there a way I can do that automatically without manually re-entering the dates again?

Extracting Field Data And Putting Each Field Into A Shell Variable?
In a MySQL query on the database (one table with 15 variable length fields,
I want to put each field into a Bash variable so that I can handle each
field as an entity.

The query I have is something like this:
mysql -e "use $database; select field1, field2, field3..., from Table1 where
fieldN like '%something%';"

can I / How do I do something like:
mysql -e "use $database; select field1,..., var1=field1, var2=field2,....;"

do
stuff with var1, var2...,
done

Create A Field By Deleting A Part Of The String From Another Field!
I have a table for "Addresses", which has a column titled "Address" and the records in this column are a combination of house#, street name and street suffix, which typically looks like "100 Main Street".

I am looking for a query result wherein a new column would be created which would have only the street name and the suffix and would eliminate the house #s, hence the result would look like "Main Street".

Copy Field Over, Then Update The Original Field
I want to change a price of a product, but back up the old price. I was trying to do this through two queries, an INSERT and an UPDATE, but it's not working.

INSERT the row that will store the backup:

INSERT
INTO
s01_MUS_SalesXProducts
(
s01_MUS_SalesXProducts.sale_id
, s01_MUS_SalesXProducts.prod_id
) VALUES (
".(int)$sale_id."
, ".(int)$prod_id."
)
Then I would copy the prices over and update the original with one UPDATE query:

UPDATE
s01_Products
LEFT
JOIN
s01_MUS_SalesXProducts
ON
s01_Products.id = s01_MUS_SalesXProducts.prod_id
SET
s01_MUS_SalesXProducts.old_price = s01_Products.price
, s01_Products.price = ".(float)$sale_price."
WHERE
s01_Products.id = ".(int)$prod_id."
However it is copying the new price to the backup field. Is there a way to do this in two queries? I know if I select the product price before inserting I can do it in 3. Truely I would like to do this in 1, but I don't believe this is possible due to the nature of the queries.

Creating Field In Table To Sum Field Values
i was wondering if there was a way in mysql to have one record that will sum up all each field value(one record with each field having sum totals)? i have a table with over 60 statistical fields that i want to get the sum for.

Updating Field With Contents Of Temporary Field
I have a table, that when new information is entered for it the new info is held in a temporary table that has exactly the same fields (the only difference is the auto increment pid in the original table is not auto increment in the temporary one - it holds the pid from the original).

I need to be able to update the original with the contents of the temporary field, all fields are updated.

Is there an easy way to do this, or will I need to select from the temporary and then run the update setting all the fields explicitly.

I have tried something like [code]
Update villas   SELECT *  FROM updatevilla WHERE pid='$villapid'
[/code ] in php but it doesn't work. I think I see what some of the problem is but am not sure how to fully write the query.

Orderby A Text Field As It Were An Integer Field
Is it possible to construct a query so that the orderby 'field' returns the rows as though the 'field' were an integer field, even though it is a text field? I do not want e.g. 10 to be before 2.

The field in question is called 'lagerantal' and is of type 'text'

Import NULL Or Empty Field Field Value
If you are importing data from an external data source and you have the choice of populating empty fields with NULL, what are the pros and cons of doing so?

Copy Partial Field Value To New Field
I have a table with state and county in one field. The first 2 characters in all the records indicates the state. Is there a way to copy just these first two characters into a new field in mysql?

Given Field Name, Find Table That Has Tha Field Name?
Is there a quick way to identify tables that have a specific column in them? For example, if I have a column name 'RuleName', how can I need to find all the tables in a database that have a column with that name?

Date Field, Time Field
I have a MySQL date field (e.g 13/12/2006) and a MySQL time field (e.g.13:00) in the same row.

I'd like to join these two fields to make a date/time field (e.g. 13:00 13/12/2006) and to be able to sort the query results on the resulting field.


Copyright © 2005-08 www.BigResource.com, All rights reserved