mysql-query

Help with sql query.

a table: +-----+-----+----+ | Sym | Pos | Id | +-----+-----+----+ | a | 0 | 0 | | b | 1 | 0 | | c | 2 | 0 | | a | 0 | 1 | | d | 1 | 1 | | b | 0 | 2 | +-----+-----+----+ need to build from this table row by Id, that would be followed in order Sym Pos. In this case, that would have: +-----+-----+...

MySQL query for sorting by multiple date values

I'm creating a PHP script that fetches IDs from a database. The results are sorted by two datetime values, createdon and publishon. The priority of publishon is higher than createdon; however, publishon may also be NULL. publishon = null => sort by createdon. Can someone help me sort those rows? ...

How to Execute a SQL statement between two seperate mySQL connections?

All, is there any means by which it is possible to connect to two seperate mySQL databases and execute SQL statements to interact with both? The aim would be to query DB 1 on connection 1 and insert the results into DB 2 on connection 2. Is it possible to do this using the mySQL query browser? Many thanks, all help appreciated ...

How do I insert into a database only if a value has changed?

I need to update (replace) fields in a MySQL database, but only if they have changed. The table contains an ID, text field and date changed. Users query the data by ID based on the date it was changed. i.e. If the date is before the last the time user queried the data, he doesn't want it. I would like to change the date in the data...

optimizing query

I have a Table foo which records the sightings of bird species. foo_id is its PK, other concerned columns are s_date, latitude and longitude. species_id is its FK. I have indexes on s_date, latitude and longitude, species_id. Table foo has 20 million records and increasing. The following query gives me top 10 latest species sightings in ...

how can i change the pattern in php?

i got this pattern for searching purpose for mysql from third party javascript. im assuming its html pattern, which can be converted, anyone have any idea on this? pattern A%5B%D9%8B-%D9%93%D9%B0%5D*L%5B%D9%8B-%D9%93%D9%B0%5D*L%5B%D9%8B-%D9%93%D9%B0%5D* ...

Create custom rollback

So I am going through our database and removing some unneeded access levels that are basically duplicates of other ones. Our database structure has a column user_level which is an enum that has a bunch of different strings (I know this isn't a great way to run user access levels but this is how the original developer made it). What I am...

How to extract the value from a resulset of two statments that are union

I have a method in dao class called "getDetails". In this method, I union the two select statments from two tables with almost same field called "main shop" & "sub shops" and put those queries to preparedstatement. Then I put the preparedStatement to resultSet. This "getDetials" method return "details" and I'll use it into another metho...

How to make search engine to run fast.

Hi, We designed a search engine but it takes much time to execute its query in drupal.We have uploaded many modules in our search engine.So could you please guide what will be the remedy for make our search engine fast.Is there any thing that i can do with my site? Thanks In Advance ...

How can I stop a running MySQL query?

I connect to mysql from my Linux shell. Every now and then I run a SELECT query that is too big. It prints and prints and I already know this is not what I meant. I would like to stop the query. Hitting Ctrl+C (a couple of times) kills mysql completely and takes me back to shell, so I have to reconnect... Is it possible to stop a query ...

How can I add headers and format MySQL query output files?

I connect to mysql from my Linux shell and use something like this: SELECT * FROM students INTO OUTFILE '/tmp/students'. Why do I see \N at line endings? I want each record in a row, but why do I see the \N explicitly printed? How can I print all column headers in the first row? ...

CONVERT and CAST in MySQL query

When I'm trying to delete an entry from a MySQL table using phpMyAdmin, I always have CONVERT and CAST in the delete query. Why do they appear and how can I get rid of them? Example: DELETE FROM `table` WHERE `table`.`field_1` = 3 AND CONVERT(`table`.`field_2` USING utf8) = CAST(0x6338643263323430623864326531373436343263613537353165363...

Mysql error that I don't expect

Error: Unknown column 'miahrose_pos104.phppos_sales.item_id' in 'on clause' I don't think this should happen because I am joining phppos_sales_items to phppos_items, but it seems the query thinks it is being joined to phppos_sales. What am I doing wrong? Query: CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT date(sale_time) ...

PHP: mysql return value "escapes" textarea

If the return value of a mysql query contains "" and / the content is displayed outside the textarea content = <a href="url">link</a> echo() is used inside the textarea <textarea><?php echo $row['value']; ?></textarea> the textarea displays <a href= and what follows it is displayed outside the textarea How do I contain all html in...

How do I output two fields in PHP?

Hello, I have a database that has two fields: current, and previous. Here is the code: <? $username="*****"; $password="*****"; $database="******"; mysql_connect(localhost,$username,$password) or die("Unable to connect to server"); mysql_select_db($database) or die( "Unable to select database"); $query='...

fix mysql query to return random row within subgroup

Hi All! I'm using the following query to randomly draw one row from the subset, for each ID1-ID2 pair, of records that have the minimum distance in time (YEAR and MMDD fields). CREATE TABLE temp4 AS SELECT * FROM temp3 GROUP BY ID1, ID2 ORDER BY DATEDIFF( CONCAT(YEAR,'-',LEFT(MMDD,2),'-',RIGHT(MMDD,2)), CONCAT(ID3_YEA...

MySQL - Getting historical "snapshots" from a history table

I have a MySQL database that has verious one to many relationship fields (eg orders and order_items) Each time an order_item is changed, the record is also inserted into the order_item_history table with the timestamp in the modified field. What I need to do is query the database to get the state of the order based on a particular date...

error in mysql query, does not return MIN value...

I'm trying to get this query to work. The query was written by "OMG Ponies" as an answer to: http://stackoverflow.com/questions/3796228/fix-mysql-query-to-return-random-row-within-subgroup The query below calculates correctly the difference in dates, but then fails to select the ROW (within ID1-ID2 pairs) with the minimum value of tha...

MySql Error 1064 - Created using MySQL WorkBench

I created this using MySQL WorkBench CREATE TABLE IF NOT EXISTS `bakasura_new`.`cities` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(255) NOT NULL COMMENT 'City Name' , `short_name` VARCHAR(255) NOT NULL COMMENT 'Short Name' , `country_id` INT(11) UNSIGNED NOT NULL , PRIMARY KEY (`id`) , INDEX `fk_cities...

Show all rows in MySQL that contain the same value

Hi there, I have a MySQL database: ID | Name 1 | Bob 2 | James 3 | Jack 4 | Bob 5 | James How would I return a list of all the columns where the same name appears more than once, eg, I'd like to return this: 1 | Bob 2 | James 4 | Bob 5 | James I've written a count query: SELECT Name, COUNT(Name) AS NumOccurrences FROM ...