select

Can I add an entry to mysql and return a column if it's not present in one query?

Can I have one query to check if an entry is in a mysql table, and if it is present, return a column, but if it's not present, insert it and return a value? I have a users table with the following layout: userID - int, auto-increment userName - varchar(100) active - tinyint(1) I want to do something like select userID from users wh...

SQL select at least X rows that are in a given time

hello, i am looking for an SQL query, that selects exactly rows ordered by date from a table. for this there is a column that contains a timestamp. and here comes the tricky part: it should not select rows that are older than a certain time, but it should still select at least X rows. so if in the given time there are not more than X ...

Select ISO 8601 Timestamp from MySQL Database

Hello, I am currently using PHP/MySQL and the jQuery timeago plugin. Basically, I need to pull my timestamp from the database and convert it to ISO 8601 format. Like this 2008-07-17T09:24:17Z Currently, my timestamps are in the database in this format: 0000-00-00 00:00:00 I have tried using timeago with my currently formatted time...

MySQL Query Select using sub-select takes too long

I noticed something strange while executing a select from 2 tables: SELECT * FROM table_1 WHERE id IN ( SELECT id_element FROM table_2 WHERE column_2=3103); This query took approximatively 242 seconds. But when I executed the subquery SELECT id_element FROM table_2 WHERE column_2=3103 it took less than 0.002s (and resulted 2 ...

Image in SELECT element

I know how to have pictures show up next to the options in a HTML dropdown form element using the CSS background-image property. However, the images do not show up on the selected element. Is there any way to do this (preferably using only CSS)? EDIT: Here is an example of the working code for the list elements. However, when the drop...

sql, order by column A and then by column B

Hi , How to write the sql so that the result can be ordered first by column A than by column B. Something like below: SELECT * FROM tbl WHERE predictor ORDER by col_A and ORDER by col_B ...

How do I select min/max dates from table 2 based on date in table 1 (without getting too much data from sums)

Related to a question I asked earlier here, I've found a problem which is eluding me (obviously). The original question was how to select a min and max date from a daily table based on a monthly table where some daily table dates could be missing. Basically what I needed was columns containing the month date (always the first), the earl...

select value from sqlserver problem

i have write code hear i have problem of selecting value from sqlserver i pass the value from nrno is geting by another page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <body> <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <% String AppURL = request.getCo...

How to run this query in mongodb?

My doc has an array field Keys Keys1 and Keys2 are two arrays I want all the docs where Keys contains any value in Keys1 AND any value in Keys2 Any advice? Thanks ...

For each row in query select top 20 from other query

Hi. I'm trying to do something and I'm not sure how to do it. I have some data like this: WITH a AS (SELECT theid, thename, thetimestamp FROM mytable) SELECT thename, TRUNC (thetimestamp, 'HH24'), COUNT (theid) FROM a group by thename,trunc(thetimestamp,'HH24') ORDER BY COUNT (theid) desc) which returns me the count grouped by...

How to get a records based on two conditions and second condition must be checked only if first one failed?

Hello! Is there a way to get a records in single query based on two conditions and second condition must be cheked only if first one failed. For example how can I get all records where time > NOW() and if there are no such records to get a record with max(id). ...

ncurses and stdin blocking problem

I have stdin in a select() set and I want to take a string from stdin whenever the user types it and hits ENTER. But select is triggering stdin as ready to read before ENTER is hit, and, in rare cases, before anything is typed at all. This hangs my program on getstr() until I hit ENTER. I tried setting nocbreak() and it's perfect reall...

Mysql - selecting year from a unix timestamp

I am using this: SELECT FROM_UNIXTIME(my_unix_timestamp_column, '%Y') AS year FROM table_name WHERE year = 2009; but it gives me an error: Unknown column 'year' in 'where clause'SELECT FROM_UNIXTIME(my_unix_timestamp_column, '%Y') AS year FROM table_name WHERE year = 2009 Both "my_unix_timestamp_column" and "table_name" are correct...

While select in mysql stored procuder

i will try a select into my Stored Procedure like a while e.g. while ( select * from t where t.c = 1 into test_t ) { var_t_e = test_t.c; } i hope i can be helped here. :) ...

get data from gridview without querying database

Hi there I am new at this so please bear with me... I have managed to get the following code to work...so when I click on the "select" link in each row of the gridview, the data is transfered to other label/textbox on the webpage. So far so good, the thing is that everytime I click on select...it goes and checks on the database for th...

SELECT (* - some_columns) FROM TABLE in SQL

Hi, I have a table with many columns among which I'd like to omit a few alone in my 'select' query. Something like select (* - columns_to_be_omitted) from myTable. Is there a way to do this, other than listing all the other columns in the query? This is a one-time manual query, so I'm not that concerned about performance. Ease of use ...

Using CreateCriteria and selecting row based on maxvalue

Hi, I want to select a row from one table in the database using CreateCriteria where I have a know value for a column, not primary key so there will be multiple rows matching. The table contains update information so we have a column that contains a version number for each row based on which along with my primary question gives a singl...

Mysql: Optimizing Selecting rows from multiple ranges (using indexes?)

My table (projects): id, lft, rgt 1, 1, 6 2, 2, 3 3, 4, 5 4, 7, 10 5, 8, 9 6, 11, 12 7, 13, 14 As you may have noticed, this is hierarchical data using the nested set model http://tr.im/EO3G. Tree pretty-printed: 1 2 3 4 5 6 7 I want to select all sub projects under project 1 and 4. I can do this with: SELECT p.id FROM projects...

problem with mutating tables

I want to create trigger which will be fired after insert into some table, let`s say user. In this trigger I want to select some records from table user, besides this one I insert to table, but then I receive error about mutating table. Is it possible to get records from this table? ...

Multiple Select Statements within IF

I could use some help to write a prog./sql construct for a report.. The Sql should first check for the prompt & then decide which "select" statements should run something like (pseudo-code) Select ACCT,LOC FROM ( IF :loc = 'MN' THEN Select acc as ACCT,location as LOC ELSE IF :loc = 'MA' THEN Select accid as ACCT,loc...