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




Selecting Unique Rows


I have a query that checks a list of users against a list of items and returns for example the following.

SELECT users.username, ratings.recording_uid
FROM users
INNER JOIN
ratings ON (users.uid = ratings.user_uid)
WHERE ((users.username = 'a') OR (users.username = 'b'))

which returns...

username recording_uid
a 1
b 1
a 2
b 2
b 3
a 4
b 4
b 5

How can I add to that query so that I know what user a has that user b doesn't and vice versa? eg. so that it returns

username recording_uid
a 3
b 5




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Help With Selecting Unique Values
I have a table called 'pictures', this holds information for every picture in an art exhibition.

Its structure is (catno, title, artistfirstname, artistsurname, size, medium,price,dimensions)

sample data would be something like.

1, birds, James, Quinn, 23*23, watercolour, 355, unsold

2, snake, David, Blake, 43*43, pencil, 499, sold

3, lion, David, Blake, 25*53, pencil, 250, unsold

4, tiger, Adam, Healy, 63*73, watercolour, 235, sold

5, fish, John, Smith, 12*15, charcoal, 450, sold

6, eagle, Peter, Blake, 54*54 pencil, 950, unsold

I need a page that lists all the exhibiting artists and was wanting to use the database to generate this rather than having to do it manually.

Artists can have multiple pictures in the system. Also some artists will share surnames. I need a statement that pulls out all the unique artists, giving a result like:

James Quinn
David Blake
Adam Healy
John Smith
Peter Blake

Buliding A Query:selecting Unique Records
I am nearly positive that there is some way to have mysql select only the first occurance of a data entity on a select.  

Maybe a better explanation is given by example.  Think of having a table of employees, with one of the columns being their bosses email address.  Now I want to do a select statement on the bosses email addresses that only returns one entry for each bosses email, even though that bosses email address will occour > 1 in the table. (more than 1 employee has the same boss...)

I could parse out the duplicates when I get to PHP but I would rather have mysql do the work for me.

Selecting Unique Values From A Table (NOT DISTINCT!)
Lets say we have a table that has the field photo_id and has values of it many times. For example 245 can exist any number of times (1, 5, 8, e.t.c.).

I want to select the values that exist in the table ONLY ONCE. Please note that i dont want to select each value once (using DISTINCT), that would return all the values that exist in the table.

For example lets say the table has the following values:

245
563
776
224
563
563
776
776
776
776

The query i want to make should return only the values 245 and 224 as 563 and 776 exist many times in that table.

Selecting Multiple Columnn Values Into 1 Distinct/unique List
I have a table with several fields for email (primary email, alternative email, contact email)

Some times, some of these fields will be blank and sometimes the same address will be enter for both primary and contact.

I use this table and these fields to do a mailout but I don't want to mail people twice because there address occurs in more than 1 column.

I need to end up with a list containing all email values from those 3 columns that is unique (no duplicates).

I can do this by checking each one and putting it into a php array but thought there may be a nicer way to do it in the SQL?

Best Way To Do This... Unique Key But 3 Rows Need It?
I have a weekly schedule table, each week should have a unique number but each week requires 3 different records (one for each game). What would be the best to create a unique identifier that is the same for the 3 records each week?

5 Unique Rows
My SQL so far is:

PHP Code:
$sql =     "SELECT
        Manufacturer.ManID,
        Manufacturer.ManName,
        Manufacturer.Active AS manactive,
        Manufacturer.ManText,
        Models.ModelID,
        Models.ModelName,
        Models.ModelText,
        Models.Active AS modelactive
        FROM Manufacturer
        INNER JOIN Models ON (Manufacturer.ManID = Models.ManID) 
        WHERE Models.ModelName UNIQUE 
        AND Manufacturer.ManID = " .$manid. "
        AND Models.ModelID = " .$modelid. "
        AND (Models.Active = 1 AND Manufacturer.Active = 1)
        LIMIT 5                
        "; 

Impose A Unique Key With 11K+ Rows
I have a logging database that is doing some work for me, but I've found a surplus of data that I want to try and get a handle of. There are two fields that tend to be duplicated across a couple rows and it would help clean up the data if I could make those two fields unique from here on.

Is it possible to create a query that will delete all of the "duplicate" rows (leaving 1 row but removing 4 or 5 "duplicates")?

CREATE:

MySQL
CREATE TABLE `SearchLog` (
  `id` INT(10) NOT NULL AUTO_INCREMENT,
  `IP` VARCHAR(25) DEFAULT NULL,
  `query` VARCHAR(50) NOT NULL DEFAULT '',
  `product_count` INT(5) UNSIGNED NOT NULL DEFAULT Ɔ',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11662 DEFAULT CHARSET=latin1 AUTO_INCREMENT=11662 ;

How Can Define A Combination Of Rows As Unique?
How can define a combination of rows as unique?

I'm using mysql and need to prevent duplicate row inserting. I don't want to define one column as unique but a combination of 3 columns.

Turning Off Allowing Multiple Non-unique Rows
I feel lazy and I am busy so i don't want to fix my perl code to only alllow insertion of unique rows. I know mysql is set to allow multiple rows of the same thing but I would like to turn that feature off. Is that possible?

Selecting Rows 10-20
Is there a way to select a certain group of 10 rows? Say, I want to have multiple pages for a huge result. I want 10 rows on each page. How would I go about getting rows 11-20 on page 2?

Selecting Rows
In my MySQL table posts, I have a field called tags. In this field, there are a bunch of words seperated by spaces (each one is a 'tag' for the posts). What I am trying to do is return all posts that have a certain tag, that tag being one of the words in the tags field.

I know how to get all rows that have an exact value of something, like all posts by a certain author, but how do I get all the rows that contain a word in the long string of tags?

Selecting Rows
Hey guys, i've got a database with allot of rows. Each row has a timestamp field, wich indicated the time that that the row was added. The timestamp field is generated by time().

Now, my question, how can I get all the rows, for specific day? So I would like all the rows from yesterday, or 12 days back, or 3534 days back.

Selecting Rows
I have a database of addresses where I need to select out all those that fall under a particular road name, ie "Fake Street".
I have the SELECT * FROM MYDATABASE WHERE part, but I am unclear on how to do the next stage. The column which holds the addresses is called "ADDRESS".
In the ADDRESS column each row has the full address (1 Fake Street, 2 Fake Street etc).
What statement can I use to select those rows which contain the street name?

Selecting Distinct Rows
I have a mysql query which I am trying to select distinct information with.

Here is the query:

Selecting Rows Not In Array?
I'm trying to pull a single random field/row out of a mysql database, based on records that the user has not viewed already.

I can either store an array of IDs in a session var or a cookie, but then how do I get mysql to exclude those IDs when selecting a random number?

I'd like to 'SELECT ID FROM Table WHERE !in_array($array) ORDER BY RAND() LIMIT 1' but of course the "in_array" php code won't work in a mysql query.

Is there a way to do this in mysql only, rather than having to iterate through the array and build a query with possibly hundreds of elements in the array:

WHERE ID!=# AND ID!=# AND ID!=#,

Selecting Rows Having Repeated Value
Imagine we've got a MySQL table with the following structure:

id int
name varchar(255)

The table contains the following data (id, name):
1 name1
2 name2
3 name3
4 name1
5 name2
6 name9
7 name1

Please help me with the query to select only those rows, in which "name" value occurs in this table more than once.

So I would like to have the following result:
1 name1
2 name2
4 name1
5 name2
7 name1

Selecting Rows Not Within Another Table
I have 2 tables Stores and Store_Projects that im trying to pull all
stores not in certian projects but belong to a customer. Heres the
design of each

CREATE TABLE `Stores` (
`storeID` int(10) unsigned NOT NULL auto_increment,
`storeMetro` int(11) NOT NULL default '0',
`storeManager` int(10) unsigned NOT NULL default '0',
`storeState` int(10) unsigned NOT NULL default '0',
`storeAddress` varchar(100) default NULL,
`storeCity` varchar(50) default NULL,
`storeZipcode` varchar(20) default NULL,
`storePhone` varchar(20) default NULL,
`projectStatus` varchar(20) default NULL,
`storeSupervisorName` varchar(100) default NULL,
`storeSupervisorPhone` varchar(20) default NULL,
`storeSupervisorEmail` varchar(255) default NULL,
`storeOtherContactName` varchar(100) default NULL,
`storeOtherContactPhone` varchar(20) default NULL,
`storeOtherContactEmail` varchar(255) default NULL,
`storeNumber` varchar(50) default NULL,
PRIMARY KEY (`storeID`),
UNIQUE KEY `storeID` (`storeID`),
KEY `storeMetro` (`storeMetro`)
)

CREATE TABLE `Store_Projects` (
`projectID` int(6) unsigned zerofill NOT NULL default '000000',
`storeID` int(11) NOT NULL default '0',
`billRate` varchar(10) NOT NULL default '0.0',
`associatePayRate` varchar(10) NOT NULL default '',
`repsNeeded` smallint(6) default NULL,
`storeServiceDateDay` smallint(2) unsigned default NULL,
`storeServiceDateMonth` smallint(2) unsigned default NULL,
`storeServiceDateYear` int(4) default NULL,
`receivedPayment` enum('Y','N') NOT NULL default 'N',
`invoiceNumber` varchar(25) default NULL,
`invoiceDate` date default NULL,
PRIMARY KEY (`projectID`,`storeID`)
)

I had designed the query like this

SELECT st.*, s.stateInitials, m.metroName, sta.stateInitials AS
metroState
FROM Stores AS st
LEFT OUTER JOIN CustomerUsers AS cu
ON cu.userID = st.storeManager
INNER JOIN States AS s
ON st.storeState = s.stateID
INNER JOIN MetroAreas AS m
ON st.storeMetro = m.metroID
INNER JOIN States AS sta
ON m.stateID = sta.stateID
INNER JOIN Store_Projects AS sp
ON sp.storeID = st.storeID
WHERE st.customerID = 4
AND sp.projectID != 000002

But of course thats not going to work if the store is not in the
Store_Projects table which it wouldnt be if its not in a specific
project. So I need a way to pull all Stores with a specific customerID
but is NOT in the Store_Projects with a specific projectID.

Can anyone explain how this should be done within the query? I could
do it by creating an array and then processing out all the stores but
I would much rather let MySQL do the work for figuring this out.

Selecting Rows Not Within Another Table
I have 2 tables Stores and Store_Projects that im trying to pull all
stores not in certian projects but belong to a customer. Heres the
design of each

CREATE TABLE `Stores` (
`storeID` int(10) unsigned NOT NULL auto_increment,
`storeMetro` int(11) NOT NULL default '0',
`storeManager` int(10) unsigned NOT NULL default '0',
`storeState` int(10) unsigned NOT NULL default '0',
`storeAddress` varchar(100) default NULL,
`storeCity` varchar(50) default NULL,
`storeZipcode` varchar(20) default NULL,
`storePhone` varchar(20) default NULL,
`projectStatus` varchar(20) default NULL,
`storeSupervisorName` varchar(100) default NULL,
`storeSupervisorPhone` varchar(20) default NULL,
`storeSupervisorEmail` varchar(255) default NULL,
`storeOtherContactName` varchar(100) default NULL,
`storeOtherContactPhone` varchar(20) default NULL,
`storeOtherContactEmail` varchar(255) default NULL,
`storeNumber` varchar(50) default NULL,
PRIMARY KEY (`storeID`),
UNIQUE KEY `storeID` (`storeID`),
KEY `storeMetro` (`storeMetro`)
)

CREATE TABLE `Store_Projects` (
`projectID`.........................................

Selecting Recursive Rows
computers(id_computer,description,id_location);
locations(id_location,description,father);

I have this two tables: computers and locations;
In locations I can insert a location and tell which one is his father, so I can have locations inside of locations.

------------------------------------------------
id_location, description, father
------------------------------------------------
1, Main Building, 0
2, HRs, 1
3, Director, 2
4, Warehouse, 0
5, Office, 4
6, Director, 5
-------------------------------------------

When I insert a new computer, I tell in which location is that computer

-------------------------------------------------------
id_computer, description, id_location
-------------------------------------------------------
1, Fujitsu-Siemens 4200, 2
2, Fujitsu-Siemens 5300, 3
3, Fujitsu-Siemens 4200, 5
4, Fujitsu-Siemens 5300, 6
------------------------------------------------

Now I would like to select all computers from main building for example.

I can not select * from computers where id_location=1 because that would not get the computers from HRs and Director. I would like to have all computers listed.

Only Selecting Specific Rows
Fields: studentID, reviewID, date

I want to get all rows in a certain date range where there are at least 3 rows for a given studentID. In other words...

Case 1: student has written 2 or fewer reviews during the date range. Don't retrieve these rows.

Case 2: student has written 3 or more reviews during the date range. Retrieve ALL of their reviews during that time. So if they wrote 5 reviews, the query will have 5 rows for their studentID.

Any ideas for this query? So far I got the date range to work using this. I'm struggling with the 3+ reviews part.
SELECT `studentID`, `reviewID`, `date` FROM `review` WHERE `date` BETWEEN '2007-10-21' AND '2007-11-09' ORDER by studentID, `date`

Any ideas?

Selecting From Multiple Rows??
i have made a voting script... every time someone votes it goes into a table called 'voting'... since people can vote on multiple things in the site there are multiple instances of each user in the 'voting' database...

if each entry has a 'userID' a 'ratingGiven' and a 'objectBeingVotedOn' field
how would i go about grabbing say the "ratingGiven" field from all of one particular user's entries .

Selecting Data In Rows
I am trying to select some data in a column but can't seem to figure it out.

the table name is "Parameters"
I want to be able to just select the "catch_addr" from the value column so all i get is the email address for the id of 33

mysql> select * from Parameters where id = 33;
+----+---------------+--------------------------------------+
| id | parameter | value |
+----+---------------+--------------------------------------+
| 33 | bounce_mess | This address no longer accepts mail. |
| 33 | catch_addr | some@email.com |
| 33 | nonexist_mail | catch |
+----+---------------+--------------------------------------+
3 rows in set (0.00 sec)

Selecting Rows In MySQL
I have a big database with about 500.000 rows. Each row consists of five columns with various information, e.g.

a,1,1,1,1
a,2,2,2,2
a,3,3,3,3
b,1,1,1,1
c,1,1,1,1

I want to select all identical values from column 1 (e.g. the a in the above example).
Then I want to copy the data in a new table, which is going to have as below:
a, (1,1,1,1),(2,2,2,2), (3,3,3,3)...

Selecting All Rows From One Table That Is Not In The Other
What I want to do is this:

select table1.id from table1,table2 where table1.id != table2.id   ....  I wish it was possible to write like that... but it doesn't I guess I can figure out the reason but anyway how should I do it? PLEAse help... example:

table1    table2
id 1      id2
id 2
id 3

select table1.id from table1,table2 where <what to write here?>

result:
id 1
id 3

Selecting A Certain Rows From A Database
if i wanted to select a certain range of rows from a database,is it possible?For example,i have a column in my database which is also a primary key and i wanted to select rows from where the primary key column is 21 - 40,would this sql statement be valid?

SELECT * FROM tbl1 WHERE id=21 TO id=40;

Selecting Distint Rows
I have a load of images in a table, and these images are split into 2 different sections within my site. Some images may appear in both sections, but most only in one or the other.
I want to be able to search the images table, and have the search results page show a list of thumbnails (this i have done already).
If an image appears in both sections of the site, I do not want to show 2 thumbnails of the same image (this is what currently happens), instead I want to show a single thumbnail, and beneath it show that it is present in both sections.
The common factor between these images that appear in both sections, is a field called image_code, which is unique to each actual image. If an image appears in both sections, it will have the same image_code.
If I use DISTINCT I will only get a single image, but I am not able to show that it appears in the other section.

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?

Selecting Rows From The Past 24 Hours
I have some data rows, and they contain date entries in a datetime formatted field - e.g.:

2006-07-05 01:30:03
2006-07-05 01:46:36
2006-07-05 01:20:41

I would like to select rows from the table for the past 24 hours

It would be something like:

SELECT fldID, fldName, fldDate
FROM
my_table
WHERE
.... fldDate is within the last 24 hours
But I'm not sure how to do it.

I've used the date_format function before, but that might not help.

Then I found the date_add function - so if I did this:

SELECT DATE_ADD(&#55614;&#57150;-01-02 23:59:59', INTERVAL -24 HOUR);
It would return:

1998-01-01 23:59:59

That looks good - but I'm not sure how I can build it into my statement to only return rows where the data_format field contains data less entered in the last 24 hours.

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:

SQL Selecting Rows, Group By Increment
Lets assume the follow database:

| ProductID | Cost |
+-----------+------+
| 1 | 100 |
| 2 | 10 |
| 3 | 250 |
| 4 | 900 |
| 5 | 50 |
| 6 | 60 |
| 7 | 340 |
| 8 | 100 |
+-----------+------+

Is there an SQL function to group those values by a certain increment? For example a predefined increment of 100 would return:

| COUNT | Cost |
+-----------+---------+
| 5 | 0-100 |
| 0 | 101-200 |
| 1 | 201-300 |
| 1 | 301-400 |
| 0 | 401-500 |
| 0 | 501-600 |
| 0 | 601-700 |
| 0 | 701-800 |
| 1 | 801-900 |
+-----------+---------+

Of course, I could loop through each increment with PHP and query each increment but that could get rather slow after a while.

Selecting Rows Where Foreign Key Is Null
I have this following query:

SELECT l.NAAM, l.VOORNAAM, l.ADRES, l.LAND, l.POSTCODE, l.GEMEENTE, r.titel
FROM westhoek.wtc_aansluitingen a, westhoek.wtc_leden l, westhoek.wtc_relatietitels r
WHERE l.ID = a.lid AND l.RELATIETITEL = r.id AND a.jaar=2003 AND l.RELATIE Is Null
ORDER BY l.NAAM, l.VOORNAAM

but I also want to get the rows where l.relatietitel is null,
so in Oracle SQL it would be ... and l.relatietitel = r.id (+)....

but the (+) doesn't seem to work in mysql...

Selecting Rows From One Table With A Key That Doesn't Exist In Another.
I'm trying to select rows from one table that does not have a key in another table.

For instance:

I have a hospital table with two columns, id and name.

I have a patients table with three columns, id, name, and locationid.

Now I want to find those hospitals that have no patients. So I want to get those hospitals whos id does not equal any of the locationid's in the patients table.

Selecting Rows From Multiple Tables
If i have 2 tables with a column 'users'
is there a way to select table1.users from table1 where table1.users is not in table2.users?

Selecting Random Rows In A VERY Large Table?
I would like to select random rows in a very large mysql table but I don't want to use something like that:

"SELECT * FROM tablename
ORDER BY RAND() LIMIT 1000";
Because it's horribly slow when you have a large table like mine (6 million rows+).




Selecting Rows Whose Values Begin With A Certain Character
Let's say my table just contains just one column of words:

apple
bear
banana
water
salt
sugar
cat
zebra

How would I write a select statement that selects all rows where the word begins with the letter b, s or z?

SELECT word FROM mytable WHERE word (code goes here)

Would I need a case statement for this?

Selecting Previous And Next Rows Based On One Input
let me start by what i am doing:

SELECT msgid FROM messages WHERE uid=1;
output:
+----------------------------------+
| msgid |
+----------------------------------+
| 1588ad275a80f22e6206364abbba530a |
| 37ce77bea4481ab7ed2625055512467e |
| 4bf7e4cb13590da24625821c795eb8d7 |
| 57628792ba507161e3f73daff3c972b7 |
| 91e95b3fbe9f04467023cfe84e6ed6e2 |
| e710bb38be4f3cbd05b6db594f7e8805 |
+----------------------------------+

how would i get it so if i enter a value it will return the previous and next rows.

SOME_SQL WHERE msgid='57628792ba507161e3f73daff3c972b7' AND uid=1;
output:
+----------------------------------+
| msgid |
+----------------------------------+
| 4bf7e4cb13590da24625821c795eb8d7 |
| 57628792ba507161e3f73daff3c972b7 |
| 91e95b3fbe9f04467023cfe84e6ed6e2 |
+----------------------------------+

Selecting A MINIMUM And MAXIMUM Number Of Rows.
I have a problem where I want to get no more than 10 (LIMIT) of the newest records from a table that are within a month, but I want to get at MINIMUM 3 records, all using SQL.

I can do :

-------------------
SELECT *
FROM updates
WHERE date_of_update
BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 1
MONTH ) AND CURDATE( )
ORDER BY date_of_update DESC
LIMIT 0 , 10
-------------------

To limit it to the 10 updates no older than a month, but if there are no updates within a month I get nothing.

So I would like to do something like :

-------------------
SELECT *
FROM updates
WHERE ROWNUM() < 3 OR ((date_of_update
BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 1
MONTH ) AND CURDATE( )) AND update_id IN (
SELECT *
FROM updates
ORDER BY date_of_update DESC
LIMIT 0, 10
))
-------------------

This code will take the 10 most recent updates and then starts pulling them out as long as the number of rows is less less than 3 or the update is within the date range and one of the 10 recent updates. The problem is that mySQL appears not to have any kind of rownum function.

Limit is a nice substitute except it only works for setting an upper bound, not a lower bound number of records.

Selecting Amount Of Rows With Distinct Values In Fields
how do i select the amount of rows in table that have a distinct field value.
example: in my table i have a column (column a) that sometimes has duplicate values (sometimes 2 duplicates, sometimes more, sometimes no duplicates).

how do i get the amount rows in the table that have a unique/distinct value in their column a (without the duplicates rows)?

Many-to-many Using Link Table And Selecting Only Rows That Have More Than One Link
Okay, I am not 100% sure if I am explaining this correctly, which is why I am having a hard time finding answers via a search.

Here is my db structure:

tbl_object
----------
object_pk
name
etc

tbl_category
------------
category_pk
name

tbl_object_category_link
------------------------
object_fk
category_fk

I have successfully ran queries where I am looking for objects in just one category using:

SELECT tbl_object.name FROM tbl_object, tbl_category WHERE object_pk=object_fk AND category_pk="2"

However, if I want to only get objects that are in multiple categories, I am stuck. I can either pull ones that are in either/or like this:

SELECT tbl_object.name FROM tbl_object, tbl_category WHERE object_pk=object_fk AND (category_pk="2" OR category_pk="1")

The next "logical" step for me was to to this:

SELECT tbl_object.name FROM tbl_object, tbl_category WHERE object_pk=object_fk AND category_pk="2" AND category_pk="1"

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.

Unique ID
Say I wanted a membership site. A user would register and get a auto created ID.. Starting from 1..

I want people to buy a User ID if they want..
Say I get the ID 567 but want to purchase the ID 5555 because I like the number. But its not created yet since there arent that many users.

Is this possible with an auto icrement ID setting for the users without messing up the flow of the auto increment field if so how would I go about it.

Unique
How can i define in mysql table that a field is unique??

Set A Unique Row
I have a website which is database driven (it's a world cup prediction league).

The problem i have is that if a user changes their predictions, the script reposts every prediction again in the database.

The database table name is:
plpredictiondata

This consists of 6 rows. The ones i am interested in are called:
userid
matchid
predtime

What i need to do, is every time that predictions are posted, the row matchid should only contain a unique number per userid. Does that make sense??

As an example, say i have 2 users (userid=1, userid=2), and 2 matches (matchid=1, matchid=2). Userid=1 posts scores for both matches, then subsequently changes them. At the moment, it will show userid=1 as having matchid=1 & 2 in the table twice. I want it to only allow the most recent prediction. I have tried using REPLACE INTO, but thats not working.

UNIQUE
I am trying to create a statement where it will return the UNIQUE values
from a table, I think its the UNIQUE statment.

So a table may have 60 entries, 20 of them are X, 20 Y and 20 Z
I want it to return only X,Y,Z e.g. 3 returned entries.

Unique Value
I have a table like this:

CREATE TABLE IF NOT EXISTS `test` (
`id` INT(11) unsigned NOT NULL auto_increment,
`cid` VARCHAR(10) NOT NULL,
`item` VARCHAR(10) NOT NULL,
`val` VARCHAR(10) NOT NULL,
`dt` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1

How do I find the unique highest value in `val` ?


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