all rows in table have type field. It is either 0 or 1.
I need to count rows with 0 and with 1 in one query. So that result is something like:
type0 type1
1234 4211
How it can be implemented?
As the title describes all
we can declare cursor like this
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
Can we use join query instead off simple query?
I've got 3 tables that are something like this (simplified here ofc):
users
user_id
user_name
info
info_id
user_id
rate
contacts
contact_id
user_id
contact_data
users has a one-to-one relationship with info, although info doesn't always have a related entry.
users has a one-to-many relationship with contacts, although contacts doesn't always have related entries.
I know I can grab the proper 'users' + 'info' with a left join, is there a way to get all the data I want at once?
For example, one returned record might be:
user_id: 5
user_name: tom
info_id: 1
rate: 25.00
contact_id: 7
contact_data: 555-1212
contact_id: 8
contact_data: 555-1315
contact_id: 9
contact_data: 555-5511
Is this possible with a single query? Or must I use multiple?
i have a problem with "and" "or" syntax!!!
here is my query. . .
SELECT
`act1`.`name`,
`act1`.`surname`,
`act2`.`name`,
`act2`.`surname`,
`act3`.`name`,
`act3`.`surname`
FROM
videos,
actors AS act1,
actors AS act2,
actors AS act3
WHERE
videos.ident = 'somethink'
AND
(
act1.id = videos.id_actor1
OR
act2.id = videos.id_actor2
OR
act3.id = videos.id_actor3
)
it returns me all variations of {name, surname}, but why!!!
i want the name and surname of first axactly, second and thirth, if they exist!!!
halp me please
Is there a software like PhpMyAdmin which can be configured to access only a single database instance in a server. I cannot use PhpMyAdmin because it allows access to all databases. A view that can allow a user to create/delete/insert/update tables should do. I remember using a software similar to this but dont remember what it was. It would also be great if its in PHP.
I have some PHP code which stores whatever is typed in a textbox in the databse. If I type in bob's apples, it gets stored in the database as bob's apples.
What can be the problem?
The table storing this has the collation of latin1_swedish_ci.
I can't figure out what to do to make this work. Here's my code:
char* testStr = " trim this ";
char** pTestStr = &testStr;
trim(pTestStr);
int trim(char** pStr)
{
char* str = *pStr;
while(isspace(*str)) {
(*pStr)++;
str++;
}
if(*str == 0) {
return 0;
}
char *end = str + strlen(str) - 1;
while(end > str && isspace(*end))
end--;
*(end+1) = 0;
return 0;
}
I get an access violation on *(end+1) = 0;, but I can't declare my testStr[] as such to avoid that, because I can't pass the pointers that way. Any ideas?
hello I need to know any way to delete duplicated items for specified sid on table.
how can I do this with SQL queries ?
DELETE (DUPLICATED TITLES) FROM table WHERE SID = "1"
something like this but i don't know true one :/ thanks.
I need to split some info from a asterisk delimitted data.
Data Format:
NAME*ADRESS LINE1*ADDRESS LINE2
Rules:
1. Name should be always present
2. Address Line 1 and 2 might not be
3. There should be always three asterisks.
Samples:
MR JONES A ORTEGA*ADDRESS 1*ADDRESS2*
Name: MR JONES A ORTEGA
Address Line1: ADDRESS 1
Address Line2: ADDRESS 2
A PAUL*ADDR1**
Name: A PAUL
Address Line1: ADDR1
Address Line2: Not Given
My algo is:
1. Iterate through the characters in the line
2. Store all chars in a temp variables until first * is found. Reject the data if no char is found before first occurence of asterisk. If some chars found, use it as the name.
3. Same as step 2 for finding address line 1 and 2 except that this won't reject the data if no char is found
My algo looks ugly. The code looks uglier. Spliting using //* doesn't work either since name can be replaced with address line 1 If the data is *Address 1*Address2, split will create two indexes in the array where index 0 will have the value of Address 1 and index 2 will have the value of Address2. Where's the name. Was there a name? Any suggestion?
Preliminary:
I have an application which maintains a thread pool of about 100 threads. Each thread can last about 1-30 seconds before a new task replaces it. When a thread end, that thread almost always will result in inserting 1-3 records into a table, this table is used by all of the threads. Right now, no transactional support exists, but I am trying to add that now. So...
Goal
I want to implement a transaction for this. The rules for whether or not this transaction commits or rollback reside in the main thread. Basically there is a simple function that will return a boolean.
Can I implement a transaction across multiple connections?
If not, can multiple threads share the same connection? (Note: there are a LOT of inserts going on here, and that is a requirement).
Hi guys,
I've got this code
mysqli_query ( $userdatabase,
'CREATE TABLE `user_'.$emailreg.'` (
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
IP varchar(10),
FLD1 varchar(20),
FLD2 varchar(40),
FLD3 varchar(25),
FLD4 varchar(25),
FLD5 varchar(25) )' );
echo ( mysqli_error ($userdatabase) );
that works fine on my localhost, but when I upload it to the server, it starts giving me a "Incorrect table name '[email protected]'" error. any idea?
Thanks!
I have a procedure SelectProc wich contains SELECT statement. I want to add a procedure param LimitRowsCount and use it as following:
CREATE PROCEDURE SelectProc (IN LimitRowsCount INTEGER UNSIGNED)
BEGIN
SELECT (...)
LIMIT LimitRowsCount;
END
but this approach doesn't work.
The SELECT itself contains nested subqueries so I can't create view from it. Is there a way more propper then dynamic SQL (prepared statements) ?
Hi,
I am trying to select between two dates, but in the table they have timestamps after them.
Example: calldate is 2010-03-16 22:43:28
How would I select this using calldate between '$date1' AND '$date2' to wildcard the timestamp after the date?
I tried using like but it didn't work
I just want to do between '$date1%' AND '$date2%'
Anyone know how to do this?
Thank you in advance!
Got this:
Table a
ID RelatedBs
1 NULL
2 NULL
Table b
AID ID
1 1
1 2
1 3
2 4
2 5
2 6
Need Table a to have a comma separated list as given in table b. And then table b will become obsolete:
Table a
ID RelatedBs
1 1,2,3
2 4,5,6
This does not rund through all records, but just ad one 'b' to 'table a'
UPDATE a, b
SET relatedbs = CONCAT(relatedbs,',',b.id)
WHERE a.id = b.aid
$sql = "SELECT logs.full_name, logout.status FROM logs, logout WHERE logs.employee_id = logout.employee_id";
tables -- logs
logout
I'm having error on this. I search join tables in google. And that's what I got. What is wrong with this code?
To put this question into context, I'm trying to calculate "time in app" based on an event log.
Assume the following table:
user_id event_time
2 2012-05-09 07:03:38
3 2012-05-09 07:03:42
4 2012-05-09 07:03:43
2 2012-05-09 07:03:44
2 2012-05-09 07:03:45
4 2012-05-09 07:03:52
2 2012-05-09 07:06:30
I'd like to get the difference between the highest and lowest event_time from a set of timestamps that are within 2 minutes of eachother (and grouped by user). If a timestamp is outside of a 2 minute interval from the set, it should be considered a part of another set.
Desired output:
user_id seconds_interval
2 7 (because 07:03:45 - 07:03:38 is 7 seconds)
3 0 (because 07:03:42)
4 9 (because 07:03:52 - 2012-05-09 07:03:43)
2 0 (because 07:06:30 is outside 2 min interval of 1st user_id=2 set)
This is what I've tried, although I can't group on seconds_interval (even if I could, I'm not sure this is the right direction):
SELECT (max(tr.event_time)-min(tr.event_time)) as seconds_interval
FROM some_table tr
INNER JOIN TrackingRaw tr2 ON (tr.event_time BETWEEN
tr2.event_time - INTERVAL 2 MINUTE AND tr2.event_time + INTERVAL 2 MINUTE)
GROUP BY seconds_interval
Hey guys I have a query that selects data and organizes but not in the correct order. What I want to do is select all the comments for a user in that week and sort it by each topic, then sort the cluster by the latest timestamp of each comment in their respective cluster. My current query selects the right data, but in seemingly random order. Does anyone have any ideas?
select * from (
SELECT
topic.topic_title, topic.topic_id
FROM comments
JOIN topic ON topic.topic_id=comments.topic_id
WHERE comments.user='$user' AND comments.timestamp>$week order by comments.timestamp desc) derived_table
group by topic_id
First of, a simplified version: I am wondering if I can create a trigger to activate during INSERT (it's actually LOAD DATA INFILE) and NOT enter records for an RMA already in my table?
I have a table that has no records that are unique. Some may be duplicates but there is one field that I can use to know if the data has been entered or not. For instance
RMA Op Days
---------------------
213 Repair 0.10
213 Test 0.20
213 Repair 0.10
So I could do an index on the three columns together but as you see it's possible for an RMA to be in a step for the same amount of time twice so it's possible to have duplicate records. This data comes from a report that I cannot edit and this is all it provides. The key is that an RMA's data is only in the report once so if my database already has that RMA in it's records I want to skip the loading of that RMA's records from the report.
By all means please let me know if that didn't make sense, I'll Explain as needed. I'm sure it's not uncommon but I couldn't find anything on the net.
I have a large table (of sites) with several numeric columns - say a through f. (These are site rankings from different organizations, like alexa, google, quantcast, etc. Each has a different range and format; they're straight dumps from the outside DBs.)
For many of the records, one or more of these columns is null, because the outside DB doesn't have data for it. They all cover different subsets of my DB.
I want column t to be their weighted average (each of a..f have static weights which I assign), ignoring null values (which can occur in any of them), except being null if they're all null.
I would prefer to do this with a simple SQL calculation, rather than doing it in app code or using some huge ugly nested if block to handle every permutation of nulls. (Given that I have an increasing number of columns to average over as I add in more outside DB sources, this would be exponentially more ugly and bug-prone.)
I'd use AVG but that's only for group by, and this is w/in one record. The data is semantically nullable, and I don't want to average in some "average" value in place of the nulls; I want to only be counting the columns for which data is there.
Is there a good way to do this?
Ideally, what I want is something like UPDATE sites SET t = AVG(a*a_weight,b*b_weight,...) where any null values are just ignored and no grouping is happening.
I have a resultset of lets say 10 results.
3 of them have a type 'Pears', and the next 3 have a type 'Apples' and the next three have a type of 'Bananas'. The last record has a type of 'Squeezing Equipment' - unrelated to the fruits.
How do I return this set of results (for pagination too) in a GROUPED order that I specify WITHOUT using any inherent sort factor like ALPHABETA or ID etc?
I have the all types at my disposal before running the code so they can be specified.
i.e.
ID | Bananas
ID | Bananas
ID | Bananas
ID | Apples
ID | Apples
ID | Apples
ID | Pears
ID | Pears
ID | Pears
ID | Squeezing Equipment
Table #1: qa_returns_items
Table #2: qa_returns_residues
I have a long time trying to get this Result:
item_code - item_quantity
2 - 1
3 - 2
IF qa_returns_items.item_code = qa_returns_residues.item_code AND status_code = 11 THEN
item_quantity = qa_returns_items.item_quantity -
qa_returns_residues.item_quantity
ELSEIF qa_returns_items.item_code = qa_returns_residues.item_code AND status_code = 12 THEN
item_quantity = qa_returns_items.item_quantity +
qa_returns_residues.item_quantity
ELSE
show diferendes
END IF
I tried this Query:
select SubQueryAlias.item_code,
total_ecuation, SubQueryAlias.item_unitprice,
SubQueryAlias.item_unitprice * total_ecuation as item_subtotal,
item_discount,
(SubQueryAlias.item_unitprice * total_ecuation) - item_discount as item_total
from (
select
ri.item_code
, case status_code
when 11 then ri.item_quantity - rr.item_quantity
when 12 then ri.item_quantity + rr.item_quantity
end as total_ecuation
, rr.item_unitprice
, rr.item_quantity
, rr.item_discount * rr.item_quantity as item_discount
from qa_returns_residues rr
left join qa_returns_items ri
on ri.item_code = rr.item_code
WHERE ri.returnlog_code = 1
) as SubQueryAlias
where total_ecuation > 0 GROUP BY (item_code);
The query returns this result:
item_code - item_quantity
1 - 2
2 - 2
I have a movie database Kind of like a blog and I want to display the last 4 created entries. I have a column in my table for timestamp called 'dateadded'. Using this code how would I only display the 4 most recent entries to table
<?php
//connect to database
mysql_connect($mysql_hostname,$mysql_user,$mysql_password);
@mysql_select_db($mysql_database) or die("<b>Unable to connect to specified database</b>");
//query databae
$query = "SELECT * FROM movielist";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$imgurl=mysql_result($result,$row,"imgurl");
$imdburl=mysql_result($result,$row,"imdburl"); ?>
<div class="moviebox rounded"><a href="http://<?php echo $domain; ?>/viewmovie?movieid=<?php echo $id; ?>" rel="facebox">
<img src="<?php echo $imgurl; ?>" />
<form method="get" action="">
<input type="text" name="link" class="link" style="display:none" value="http://us.imdb.com/Title?<?php echo $imdburl; ?>"/>
</form>
</a></div>
<?php
$row++;
}
?>