When I create a view I am basically like making a new table that will automatically be transacted upon when data in one of the tables it joins changes?
Also why can't I use subqueries in my view????
I am doing a query with three tables, the problem was one table has many occurrences of id of another.
sample data:
users: id
answers:
id:1
user_answer :1
id:1
user_answer :2
id:1
user_answer :3
Questions:
id:1
answers :answer description
id:2
answers :answer description
id:3
answers :answer description
How can I get all user information and all answer and its description, I used GROUP by user.id but it only returns only one answer.
I want to return something like this list all of users answer:
Name Q1 Q2
USERNAME ans1,ans2 ans1,ans2 comma separated description of answer here
I am building a note taking app for myself with tag filtering functions, but am having an issue when trying to grab notes with the tags. The tag filter needs to use AND not IN, because it will help better narrow down what I am looking for.
My tables are configured like this:
+ notes note_id | note_title | note_uid
+ tags tag_id | tag_title
+ notes_tags nt_id | nt_note_id | nt_tag_id
The notes_tags table keeps track of all notes' tags.
I am not worried about returning any information about tags, so here is an example LEFT JOIN I am using currently to only get notes with only 1 tag.
SELECT * FROM notes_tags LEFT JOIN notes ON note_id = nt_note_id WHERE note_uid IN ( 1 ) AND nt_tag_id = 10
This query runs perfect, it grabs all of the notes with that single tag. However, I am having issues "pinpointing" my notes using a query like this:
SELECT * FROM notes_tags LEFT JOIN notes ON note_id = nt_note_id WHERE note_uid IN ( 1 ) AND nt_tag_id = 10 AND nt_tag_id = 11
What am I doing wrong with the syntax?
Hello!
How do i get the actual max length of a specified column in php?
For instance, this table:
id - int(11)
name - string(20)
I want in php to select the maximum number of characters that a field can have, like
SELECT length(name) from table1
and it should then return 20 (since its the maximum number of characters for that field).
I'm way out of my league here...
I have a mapping table (table1) to assign particular values (value) to a whole number (map_nu). My second table (table2), is a collection of averages (avg)
(I couldn't figure out how to properly make a markdown table, please feel free to edit!)
table1: table2:
(value)(Map_nu) (avg)
---- -----
1 1 1.111
1.045 2 1.2
1.09 3 1.33333
1.135 4 1
1.18 5 1.389
1.225 6 1.42
1.27 7 1.07
1.315 8
1.36 9
1.405 10
I need to find a way to match the averages from table2 to the closest value in table1. It only need to match to the 2 digit past the decimal, so I've added the Truncated function
SELECT map_nu
FROM `table1`
JOIN table2 ON TRUNCATE(table1.value,2)=TRUNCATE(table2.avg,2)
I still miss the values that don't match the averages exactly. Is there a way to pick the nearest truncated value?
Thanks!
I HAVE TWO TABLES shown below
table_joining
id join_id(PK) transfer_id(FK) unit_id transfer_date joining_date
1 j_1 t_1 u_1 2010-06-05 2010-03-05
2 j_2 t_2 u_3 2010-05-10 2010-03-10
3 j_3 t_3 u_6 2010-04-10 2010-01-01
4 j_5 NULL u_3 NULL 2010-06-05
5 j_6 NULL u_4 NULL 2010-05-05
table_transfer
id transfer_id(PK) pastUnitId futureUnitId effective_transfer_date
1 t_1 u_3 u_1 2010-06-05
2 t_2 u_6 u_1 2010-05-10
3 t_3 u_5 u_3 2010-04-10
now i want to know total employee detalis( using join_id) which are currently working on unit u_3 .
means i want only
join_id
j_1 (has transfered but effective_transfer_date is future date, right now in u_3)
j_2 ( tansfered and right now in `u_3` bcoz effective_transfer_date has been passed)
j_6 ( right now in `u_3` and never transfered)
what i need to take care of below steps( as far as i know )
<1> first need to check from table_joining whether transfer_id is NULL or not
<2> if transfer_id= is NULL then see unit_id=u_3 where joining_date <=CURDATE() ( means that person already joined u_3)
<3> if transfer_id is NOT NULL then go to table_transfer using transfer_id (foreign key reference)
<4> now see the effective_transfer_date regrading that transfer_id whether effective_transfer_date<=CURDATE()
<5> if transfer date has been passed(means transfer has been done) then return futureUnitID otherwise return pastUnitID
i used two separate query but don't know how to join those query??
for step <1 ans <2
SELECT unit_id FROM table_joining WHERE joining_date<=CURDATE() AND transfer_id IS NULL AND unit_id='u_3'
for step<5
SELECT IF(effective_transfer_date <= CURDATE(),futureUnitId,pastUnitId) AS currentUnitID FROM table_transfer
// here how do we select only those rows which have currentUnitID='u_3' ??
please guide me the process?? i m just confused with JOINS. i think using LEFT JOIN can return the data i need, or if we use subquery value to main query?
but i m not getting how to implement ...please help me.
Thanks for helping me alwayz
I have an query like:
SELECT id as OfferId FROM offers
WHERE concat(partycode, connectioncode) = ?
AND CURDATE() BETWEEN offer_start_date
AND offer_end_date AND id IN ("121211, 123341,151512,5145626 ");
Now I want to cache the results of this query using memcache and so my question is
How can I cache an query using memcache.
I am currently using CURDATE() which cannot be used if we want to implement caching and so how can I get current date functionality without using CURDATE() function ?
I have 3 tables. Here is the relevant information needed for each.
items
prod_id
order_id
item_qty
orders
order_id
order_date
order_status
acct_id
accounts
acct_id
is_wholesale
items is linked to order by the order_id and orders is linked to accounts via acct_id
I need to sum item_qty for all items where prod_id=464 and the order stats is not 5 and where the is_wholesale is 0 and the order_date is between two dates. Im struggling with this and would appreciate any help. Here is what I have but it's not working correctly:
SELECT SUM(items.item_qty) as qty
FROM items
LEFT JOIN orders ON orders.order_id = items.order_id
LEFT JOIN accounts on orders.acct_id = accounts.acct_id
WHERE items.prod_id =451
AND orders.order_date >= '$from_date'
AND orders.order_date <= '$to_date'
AND orders.order_status <>5
AND accounts.is_wholesale=0;
Again, any help would be greatly appreciated!
i am doing a delete with a LIKE statement
my keybuffer is 25m, the sort buffer size is 256k
the delete has been taking over 2 hours
should i increase memory usage? there are about 50 megs of data in the table from which i am deleting, thats about 500,000 rows
is there anything else i can do on the adminsitration size to speed up this delete?
I have following table :
id dateStart dateEnd active
1 2012-11-12 2012-12-31 0
2 2012-11-12 2012-12-31 0
I want to compare todays date in between dateStart and dateEnd.
Following is my query for this :
$todaysDate="2012-26-11";
$db = Zend_Registry::get("db");
$result = $db->fetchAll("SELECT * FROM `table` WHERE active=0 AND {$todaysDate} between dateStart and dateEnd");
return $result;
But its not working.
Any solution.
Thanks in advance.
Cannot add or update a child row: a foreign key constraint fails (`mydb`.`job_listing_has_employer_details`, CONSTRAINT `job_listing_has_employer_details_ibfk_2` FOREIGN KEY (`employer_details_id`) REFERENCES `employer_details` (`id`))
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`) VALUES (6, '5')
What does this mean? The two ID's I am inserting into the table exsist.
I am trying to write a query to select all records from users table where User_DateCreated (datetime field) is = 3 months from today.
Any ideas? Thanks!
I am trying to install magento (open source e-commerce platform) sample data on my webhost.
I have uploaded the file magento_sample_data.sql via ftp, and setup a new database and have assigned it to a user.
How do I get the sample data into my empty database?
I have a DB with a table that is named "victim". The form that dumps the info into the table has room for two victims and therefore there is vic1_fname, vic1_lname, vic2_fname, vic2_lname, etc.. (business name, person first, person last, address, city, state, zip) a "1" and "2" of each. Now I want to search the DB and locate listed victims.
This is what I have so far:
$result = mysql_query(
"SELECT victim.*
FROM victim
WHERE vic1_business_name OR vic2_business_name LIKE '%$search_vic_business_name%'
AND vic1_fname OR vic2_fname LIKE '%$search_vic_fname%'
AND vic1_lname OR vic2_lname LIKE '%$search_vic_lname%'
AND vic1_address OR vic2_address LIKE '%$search_vic_address%'
AND vic1_city OR vic2_city LIKE '%$search_vic_city%'
AND vic1_state OR vic2_state LIKE '%$search_vic_state%'
AND vic1_dob OR vic2_dob LIKE '%$search_vic_dob%'
");
<table width="960" style="border: groove;" border=".5">
<tr><th colspan=10>You search results are listed below:</th></tr>
<tr>
<th>Case Number</th>
<th>Business Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>DOB / Age</th>
<th>Address</th>
<th>City</th>
<th>State</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{ ?>
<tr>
<td align="center"><?php print $row['vic_business_name']; ?></td>
<td align="center"><?php print $row['vic_fname']; ?></td>
<td align="center"><?php print $row['vic_lname']; ?></td>
<td align="center"><?php print $row['vic_dob']; ?></td>
<td align="center"><?php print $row['vic_adress']; ?></td>
<td align="center"><?php print $row['vic_city']; ?></td>
<td align="center"><?php print $row['vic_state']; ?></td>
</tr>
<?php } ?>
</table>
The info did not display in the table until I changed the table to this:
<tr>
<td align="center"><?php print $row['vic1_business_name']; ?></td>
<td align="center"><?php print $row['vic1_fname']; ?></td>
<td align="center"><?php print $row['vic1_lname']; ?></td>
<td align="center"><?php print $row['vic1_dob']; ?></td>
<td align="center"><?php print $row['vic1_adress']; ?></td>
<td align="center"><?php print $row['vic1_city']; ?></td>
<td align="center"><?php print $row['vic1_state']; ?></td>
</tr>
<tr>
<td align="center"><?php print $row['vic2_business_name']; ?></td>
<td align="center"><?php print $row['vic2_fname']; ?></td>
<td align="center"><?php print $row['vic2_lname']; ?></td>
<td align="center"><?php print $row['vic2_dob']; ?></td>
<td align="center"><?php print $row['vic2_adress']; ?></td>
<td align="center"><?php print $row['vic2_city']; ?></td>
<td align="center"><?php print $row['vic2_state']; ?></td>
</tr>
Now it displays both rows, even if its empty. It doesn't matter if the victim was listed originally as vic1 or vic2, i just want to know if they are a victim.
I hope this makes sense. I can't get it to display the way I want, line-by-line, irregardless of whether you are vic1 or vic2.
I've got a monitoring system that is collecting data every n seconds (n ~=10 but varies). I'd like to aggregate the collected data by 15 minute intervals. Is there a way to corral the timestamp column into 15 minute chunks to allow for grouping to work?
Hi guys, thanks in advance for any help on this topic!
I'm sure this has a very simply answer, but I can't seem to find it (not sure what to search on!). A standard count / group by query may look like this:
SELECT COUNT(`t2`.`name`)
FROM `table_1` `t1`
LEFT JOIN `table_2` `t2` ON `t1`.`key_id` = `t2`.`key_id`
GROUP BY `t1`.`any_col`
and this works as expected, returning 0 if no rows are found. So does:
SELECT COUNT(`t2`.`name`)
FROM `table_1` `t1`
LEFT JOIN `table_2` `t2` ON `t1`.`key_id` = `t2`.`key_id`
WHERE `t1`.`another_column` = 123
However:
SELECT COUNT(`t2`.`name`)
FROM `table_1` `t1`
LEFT JOIN `table_2` `t2` ON `t1`.`key_id` = `t2`.`key_id`
WHERE `t1`.`another_column` = 123
GROUP BY `t1`.`any_col`
only works if there is at least one row in table_1 and fails miserably returning an empty result set if there are zero rows. I would really like this to return 0! Anyone enlighten me on this? Beer can be provided in exchange if you are in London ;-)
In my query:
$cselect = mysql_real_escape_string($_POST['cselect']);
---------------
---------------
$sql = sprintf("INSERT INTO content
(id, catID, title, abstract, body, status, published, date, description_meta, keywords_meta)
VALUES ('', '%s', '%s','%s','%s','%s','%s','%s','', '' )", $cselect,$chead, $cabst,$ctext, $cp, $cradio, 'TIMESTAMP: Auto NOW()');
ouptput for date is: 0000-00-00 00:00:00. What is wrong in my query?
Thanks in advance
I am calling the results form a query to a list on my site based on if the item is "downtown_hosted". This works fine but I would now like to sort that list DESC, but can't seem to get the syntax correct.
Below is what I have:
$result_events = mysql_query("SELECT * FROM events WHERE downtown='downtown_hosted' ORDER BY date DESC LIMIT 5 ");
I have been looking for a while now but I can not find an easy solution for my problem. I would like to duplicate a record in a table, but of course, the unique primary key needs to be updated.
I have this query:
INSERT INTO invoices
SELECT * FROM invoices AS iv WHERE iv.ID=XXXXX
ON DUPLICATE KEY UPDATE ID = (SELECT MAX(ID)+1 FROM invoices)
the problem is that this just changes the ID of the row instead of copying the row. Does anybody know how to fix this ?
Thank you verrry much,
Digits
//edit: I would like to do this without typing all the field names because the field names can change over time.
ID
NAME
AMT
1
Name1
1000
2
Name2
500
3
Name3
3000
4
Name1
5000
5
Name2
2000
6
Name1
3000
consider above table as sample.
am having a problem in my sql query, Am using like this.
Select name,amt from sample where amt between 1000 and 5000
it returns all the values in the table between 1000 and 5000, instead
I want to get maximum amount record for each name
i.e.,
3 name3 3000
4 name1 5000
5 name2 2000
Hi,
I inserted a number of rows 3 hours ago and I don't want these rows from changing. How can I write sql statement that compare current time with the timstamp in the row and restrict users from changing it if abouve creteria is met.
Thanks
I have the following query that works well.
SELECT DISTINCT city,region1,region2 from static_geo_world where country='AU' AND
(city LIKE '%geel%' OR region1 LIKE '%geel%' OR region2 LIKE '%geel%' OR region3 LIKE '%geel%' OR zip LIKE 'geel%') ORDER BY city;
I need to also extract a column named 'id' but this messes up the DISTINCT as each ID is different.
How can I get the same UNIQUE set of records as above but also get the 'id' for each record?
Note: sometimes I can return a few thousand records so a query for each record isn't possible.
Any ideas would be very welcome...
I am working on a widget that is a lot like twitters widget where there is a list of postings and a view more button. I can get it to work with using ID variables but I would like to sort by popular posts.
Here is my mysq code:
$sql = "SELECT id, title, category, icon_normal, status, description, views_monthly FROM posts WHERE views_monthly<=".$lastPost." AND status='1' ORDER BY views_monthly DESC LIMIT 9"
So the problem that I am having is it shows the first 9 just fine. When it gets to the point where views_monthly = 0 then it just loads the same 9 post over again.
How do it get it to switch to using ID when it reaches Views_monthly = 0 and load fresh posts?
UPDATE counter_reports
SET `counter`=`counter`+1,`date`=?
WHERE report_id IN(
(SELECT report_id FROM counter_reports WHERE report_name="emails_sent" AND `year`=1 ORDER BY report_id DESC LIMIT 1),
(SELECT report_id FROM counter_reports WHERE report_name="emails_sent" AND `month`=1 ORDER BY report_id DESC LIMIT 1),
(SELECT report_id FROM counter_reports WHERE report_name="emails_sent" AND `week`=1 ORDER BY report_id DESC LIMIT 1),
(SELECT report_id FROM counter_reports WHERE report_name="emails_sent" AND `day`=1 ORDER BY report_id DESC LIMIT 1)
)
Is there any alternative for such sql? I need to update(increment by 1) last counter reports for day,week,month and year.
If I'm adding manually, sql works fine, but with subqueries it fails to launch.
Thanks. :)
in my forum i have threads and replies.
one thread has multiple replies. but then, a reply can be a reply of an reply (like google wave). because of that a reply has to have a column "reply_id" so it can point to the parent reply. but then, the "top-level" replies (the replies directly under the thread) will have no parent reply.
so how can i fix this? how should the columns be in the reply table (and thread table).
at the moment it looks like this:
threads:
id
title
body
replies:
id
thread_id (all replies will belong to a thread)
reply_id (here lies the problem. the top-level replies wont have a parent reply)
body
what could a smart design look like to enable reply a reply?