mysql-query

Where i write MySQL trigger

Hi all, I didn't write any trigger for my works. Now i want to know how to write trigger . And where it write . Is it possible to write trigger as sql query in phpmyadmin .. Please help me to write a simple trigger... I tried like below Create Trigger sales_bi_trg BEFORE INSERT ON sales FOR EACH ROW BEGIN DECLARE num_row INTEGER ; D...

refer to complex expression value in where clause

I have a query where I have a complex date expression as one of the columns. SELECT date_column + INTERVAL( complex_jimmy_jam ) DAY AS complex_date_calculation FROM table I want to refer to that column in the where clause WHERE complex_date_calculation < NOW() But mysql chokes on it. 1054: Unknown column 'complex_date_calcul...

mysql_query returns an empty resultset, even when query is valid

Hi! When I execute the following code in a php script. The result set is always empty. However, when I execute the same query in mysql console or in phpmyadmin,it rightfully shows 18 rows in the resultset. What could be going wrong here. $result_set = mysql_query("SELECT * FROM categories WHERE 1 ", $this->database_handle); var_dump($...

SQLite delete from table where field does not match an object in an array

Say I have a table with an ID field, and I have a local array (in any language, php or objective c or basic or whatever) of eligible IDs. I want to delete any record in which the ID field does not match any of the eligible IDs in my array. Is there any way to do this in a single SQL query? Can you pass an array into SQL and basically sa...

Query with Ruby on Rails

Hi: I'm struggling with a rails query. Finally, I want to make a JSON response with the following contents: A listing of all countries which have at least one company, associated through "Address" and a count of how of how many companies reside in a country. Here is my simple model structure: class Country < ActiveRecord::Base has_...

Need help on a MySQL search query

Hi, I need some help on a MySQL query I'm trying to set up. I need to find records that match conditions that are located in two different tables with a many to many relationship. I use three tables in this query, the first contains projects, the second contains topics and the third ties them together. I want the query to find projects...

MySQL concatenating fields but ignoring empty ones

I have a MySQL db with a list of people, including their address, which I want to return as one field in a query. It's split into address1, address2, address3, address4, post_code and I want to do something like the following SELECT CONCAT(`address1`, ' ', `address2`, ' ', `address3`, ' ', `address4`, ' ', `post_code`) AS `address` FRO...

Ultra-grand super acts_as_tree rails query

Right now I'm dealing with an issue regarding an intense acts_as_tree MySQL query via rails. The model I am querying is Foo. A Foo can belong to any one City, State or Country. My goal is to query Foos based on their location. My locations table is set up like so: I have a table in my database called locations I use a combination of ac...

Selecting Max of two columns (revisited)

I asked a similar question before only to later discover that what I thought was the answer didn't work because I hadn't asked the right question (and hadn't noticed that the answerer told me this). To revisit, I have a table of comparisons. I'm trying to select the row with the maximum version for each set of two students. So what I had...

Why does this query return nothing?

This query works fine but I'm having trouble trying to figure out why it does not return anything if the user does not have any categories in the table "user_categories"? This is the table structure: users: user id, username, user city categories: category id, category name user_categories: user id, category id SELECT users.*, GROUP...

MySQL JOIN / GROUP_CONCAT second table?

Hello! So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM users LEFT OUTER JOIN user_categories ON users.user_id = user_categories.user_id LEFT OUTER JOIN categories ON user_categories.category_id = categories.category_id WHERE users.user_city = 'brooklyn' GROUP BY users...

Normalizing a table, from one to the other

Hi, I'm trying to normalize a mysql database.... I currently have a table that contains 11 columns for "categories". The first column is a user_id and the other 10 are category_id_1 - category_id_10. Some rows may only contain a category_id up to category_id_1 and the rest might be NULL. I then have a table that has 2 columns, user_i...

Why does explain say that it uses type ALL for contests table though contest table has chid as primary key?

Why does explain say that it uses type ALL for contests table though contest table has chid as primary key? mysql> explain SELECT contests.chid, contests.diff_level from contests, contest_users where contests.chid=contest_users.chid; +----+-------------+-----------------+------+---------------------+---------------------+-...

MYSQL query to find all rows that are outside join

I am trying to create a query which will return all rows of a table CLASSES that do not matched with another table named CHILD_CLASSES. These two tables look like this, The table CLASSES is a self referencing table so the top CLASS does not have a parent_id. The Parent ID row is created from the class_id row. So when I inserted A1 as a...

Find best matching row in MySQL (InnoDB)

I have the following test string engine/mail/key and a table which looks like this: +-------------+ | query | +-------------+ | engine | | engine/pdf | | engine/mail | +-------------+ I want to find the best matching row. Best match is specified by the most matching characters from the start of the string/row. I have con...

On duplicate key ignore?

Guys I'm trying to finish this query -> my tag field is set to UNIQUE and I simply want the database to ignore any duplicate tag. INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c') ON DUPLICATE KEY IGNORE '*the offending tag and carry on*' or even this would be acceptable INSERT INTO table_tags (tag) VALUES ('tag_a'),...

Find if number is between two numbers in different fields in a MySQL database

How would you format a query on a MySQL database via PHP to find if an IP address falls between two numbers in two different fields? Numerical representation of IP address to find: 1265631252 Database format: IP FROM IP TO REGISTRY ASSIGNED CTRY CNTRY COUNTRY "1265631232","1265893375","arin","1152835200","US","USA","U...

using data from two tables in mysql by using php5

hi friends, i want to get data from a table and use in the form's field and along with some new fields data i want to save all fields data in a new table,,' then what i have to do , thanxs in advance !! ...

mysql query to sort some results but join data may or not be present

SELECT PLD_LINK.ID, PLD_LINK.TITLE, PLD_LINK.URL, PLD_CATEGORY.TITLE, TLL_SORT_STATUS.status, PLD_LINK_COMMENT.DATE_ADDED FROM PLD_LINK, PLD_CATEGORY, TLL_SORT_STATUS, PLD_LINK_COMMENT WHERE PLD_LINK.CATEGORY_ID = PLD_CATEGORY.ID AND PLD_LINK.ID = TLL_SORT_STATUS.link_id AND PLD_LINK.PAGERANK BETWEEN -1 AND 2 ...

Getting average time in seconds format in mysql

How to find out average timestamp the field timestamp in a table gettime Timestamp 2010-02-08 14:17:36 | 127.0.0.1 | 2010-02-08 14:17:30 | 127.0.0.1 | 2010-02-08 14:17:30 | 127.0.0.1 | The following query gives some number how to format it and get it in seconds. select avg(timestamp) from gettime; the above gives some random ...