mysql-query

MYSQL query for searching through ALL the fields?

As dumb as it sounds, is there a way to do something like this: select row_id from mytable where * like '%searched_text%'; By * here I mean "all the fields" in the table, instead of me specifying them one by one... ...

Returning data from multiple tables in one query even if secondary tables do not apply

I have an issue getting data from three tables, which I want to return using one query. I've done this before using a query something like this one: $query = mysql_query("SELECT maintable.`id`, maintable.`somedata`, maintable.`subtable1_id`, subtable1.`somedata`, subtable1.`subtable2_id`, subtable2.`somedata` FR...

assign same rank to row with same property in mysql

I'm using the following query to extract a random row from the ones with the minimum date difference within a subgroup (as defined by an ID1,ID2 pair). It is not working, as the steps that ranks the rows by date difference assigns rank=1 only to the first observation (i.e. even if the second has the same date difference and should recei...

mysql update multiple columns

I need to update 2 datetime columns, and I need them to be exactly the same, using mysql version 4.1.20. I'm using this query: mysql> update table set last_update=now(), last_monitor=now() where id=1; It is safe or there is a chance that the columns are update with different time, beacause of the 2 visible calls to now() ? I don't thi...

Mysql query, three tables left join with group by.

Hi, I have query which provide wrong result, am I doing any thig wrong in this query SELECT b.nBoutiqueID , b.sBoutiqueName , b.Status , SUM(bs.nViewCount) nViewCount , SUM(ps.nViewCount) nProductViewCount, ...

How to find total no of '1' in the table?

Hey Friends Here is the table details and data id data h1 h2 h3 h4 h5 h6 h7 h8 1 2010-10-10 1 1 1 1 1 1 1 1 1 2010-10-11 1 0 1 0 1 0 0 1 1 2010-10-12 0 1 1 1 1 0 0 0 what i need to know is how can i create query for following find total no of zero in h1-h8 for id 1 find total no of one in h1-h8 for id 1 find total of data in h1-h8...

sql query problem

Hi: I am trying to build a web log analyzer based to the tomcat log. And I first push the log to the database, then do some Statistics. Now I meet a problem: For a given data range for example (2010-09-20 to 2010-09-25), I have to calculate the visitors of each day,so I first split the data range day by day: Split:(2010-09-20,2010...

Inserting/Updating rows with some columns and preserve values of others

A little background information: I have a table called table_a, which has 12 columns. I want to insert or update rows with values for 6 of these columns, while I don't wanna lose the data in the other 6 columns. And I wanna do this with a parameterized query in C#. field1 is Unique. > SELECT * FROM table_a; +----+--------+--------+---...

How can I write a SQL query which ensures two columns are independently unique?

I have a MySQL table which looks like the following: user_id resource_id last_used -------------------------------- 1 1 2010-09-01 1 2 2010-09-02 1 3 2010-09-04 2 3 2010-09-08 2 2 2010-09-03 3 1 2010-09-01 3 1 ...

MySQL arrange existing table columns

How can I change the position of a certain existing column in mysql table. Ex: I want username to be at the after all the columns? or I want it before any column in my table. Thanks in advance :) ...

MySql - Select Query - Joins

I have two tables User ID UserName 1 Name1 2 Name2 3 Name3 4 Name4 5 Name5 Item ID ItemName InsertedBy UpdatedBy 1 Item1 1 4 2 Item2 3 3 3 Item3 2 5 4 Item4 5 3 5 Item5 4 5 Resultant Table required ID ItemName InsertedBy UpdatedBy 1 Item1 Name1 Name4...

MySql Query:: How to solve the problem of 's in data, when query is fired

I am facing such problem in which 's is present in data. while searching it does not shows data. I wanna remove SQL injection issue Code :: @search_condition = "" if !search_text.nil? search_field = search_text.split("-") @search_condition = "( address_books.organization_name like '#{search_text}%' or...

what index(es) needs to be added for this query to work properly?

Hi, This query pops up in my slow query logs: SELECT COUNT(*) AS ordersCount, SUM(ItemsPrice + COALESCE(extrasPrice, 0.0)) AS totalValue, SUM(ItemsPrice) AS totalValue, SUM(std_delivery_charge) AS totalStdDeliveryCharge, SUM(extra_delivery_charge) AS totalExtraDeliveryCharge, this_.type ...

Get the last entries using GROUP BY

I'm having problem with GROUP BY. It returns the first entry it could find, but I would like it to return the last entry. Is that possible? Here is my query (prepared query): SELECT stamp_user, stamp_date, stamp_type FROM rws_stamps WHERE stamp_date >= ? GROUP BY stamp_user ORDER BY stamp_date DESC My table looks like this: What I...

How to write a valid query (update with autoincrement variable) ?

I want to write query like this: SET @id := 0; UPDATE table1 SET field1 = @id + 1, @id := @id + 1 And get error message: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@id := @id + 1' at line 3 How write this query correctly? 1...

PHP Query Result From One Column to Multiple Columns

// Database Settings define('DB_HOST', '**'); define('DB_PORT', '**'); define('DB_USER', '**'); define('DB_PASS', '**'); define('DB_NAME', '**'); // Connection to Database $database = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); $sql = 'SELECT AManufactureBrand.brand, AManufactureModel.model, AManufactureEdition.edition' ...

mysql: not able to retrieve data which is in between <> symbols

Hi all, Am running a sql query with php to retrieve some data from mysql database. Everything works well except that, if the data contains symbols like < and >, then whatever data present between these symbols doesnt show in the output. for example, if the data is something like "<hello there> how are you?" then only "how are you?" i...

MySQL query not offsetting correctly

Can someone help me understand why the following query is not offsetting correctly? It's meant to select all records in the games table, and add a column with a value of 0 or 1 based on whether a record in another table (wishlist) exists with the same gameId @memberId (in plain English, get me all records from games, and mark any game t...

PHP variable within a MySql query

Okay I do apologise that I'm out of my depth here. I'm not the kind of person to rely on places like this and any help towards improving my PHP & MySQL knowledge is greatly appreciated! I have this query which is called to collect information about users who are registered on our wordpress site. $verified = $wpdb->get_results("SELECT w...

Random query execution time in test case

Hi, I'm testing a method with quite huge sql query. I has about 15 joins, one subquery with 2 joins, so it is complex. But each running takes different time. Once it is 4 second, sometimes 80, or even 200 seconds. It is standard unit test, with preparing data with FactoryGirl, and data is always the same. Is there any mysql profilers, m...