I have my table columns set like this:
likes(id, like_message, timestamp)
id is the primary key that is auto incrementing. This is the SQL that I use to add a row:
$sql = "INSERT INTO `likes` (like_message, timestamp)
VALUES ('$likeMsg', $timeStamp)";
Everything works, but now I need to throw back the id attribute of the newly inserted row. For example, if I insert a row and the id of that row is 13, I need to echo out 13 so my AJAX request can pick that up and use it.
Any help would be appreciated, as well as related code samples. Thanks :)
Lets say I have a table with 20 entries. They are sorted by date (date is a column name _) in descending order. How would I go about selecting ONLY the newest entry and the 15th oldest entry?
I am getting all 15 results by doing the following query
SELECT * FROM mytable m WHERE col1 = "zzz" ORDER BY date DESC LIMIT 15;
I'm creating a members site, and I'm currently working on the user Preference settings. Should I create a table with all the preference fields (about 17 fields) or should I include them in the main member table along with the account settings?
Is there a limit as to how many fields I should have in a table? currently the member table has about 21 fields... not sure if its okay to add another 17 more fields when I can easily just put them in another table. It'll take more coding to pull up the data though... any sugguestions?
I have the following tables:
posts (post_id, content, etc)
comments (comment_id, post_id, content, etc)
posts_categories (post_category_id, post_id, category_id)
and this query:
SELECT `p`.*, COUNT(comments.comment_id) AS cmts, posts_categories.*,comments.*
FROM `posts` AS `p`
LEFT JOIN `posts_categories`
ON `p`.post_id = `posts_categories`.post_id
LEFT JOIN `comments`
ON `p`.post_id = `comments`.post_id
GROUP BY `p`.`post_id`
There are three comments on post_id=1 and four in total. In posts_categories there are two rows, both assigned to post_id=1. I have four rows in posts.
But if I query the statement above I get a result of 6 for COUNT(comments.comment_id) at post_id=1. How is this possible? I guess the mistake is somewhere in the GROUP BY clause but I can't figure out where.
Any suggestions?
Suppose that I have 3 tables:
A) Table UsrHeader
-----------------
UsrID | UsrName
-----------------
1 | Abc
2 | Bcd
B) Table UsrDetail
-------------------------------
UsrID | UsrLoc | Date
-------------------------------
1 | LocA | 10 Aug 2012
1 | LocB | 15 Aug 2012
2 | LocA | 10 Aug 2012
C) Table Trx
-----------------------------
TrxID | TrxDate | UsrID
-----------------------------
1 | 10 Aug 2012 | 1
2 | 16 Aug 2012 | 1
3 | 11 Aug 2012 | 2
What I want to do is to show the table like:
---------------------------------------
TrxID | TrxDate | UsrID | UsrLoc
---------------------------------------
1 | 10 Aug 2012 | 1 | LocA
2 | 16 Aug 2012 | 1 | LocB
3 | 11 Aug 2012 | 2 | LocA
Notice that there is one user but different location. That's based on the UsrDetail table that the user on a specified date has moved to another location. So, it should be show the user specific location on that date on every transaction.
I have try this code but it is no luck:
SELECT trx.TrxID, trx.TrxDate, trx.UsrID, User.UsrName, User.UsrLoc
FROM trx
INNER JOIN
( SELECT UsrHeader.UsrID, UsrHeader.UsrName, UserDetail.UsrLoc
FROM UsrHeader
INNER JOIN
( SELECT UsrDetail.UsrID, UsrDetail.UsrLoc, UsrDetail.Date
FROM UsrDetail
) AS UserDetail ON UserDetail.UsrID = UsrHeader.UsrID
) AS User ON User.UsrID = trx.UsrID
AND trx.TrxDate >= User.Date
How to do that? Thanks..
I Have a script, getting text from DB and post it on other DB.
Problem is, if I have a Text lngen then 840 Words, I can't call this page. Get an error about "Not Found" or "Connection brocken" or what ever. In FF i get no error, only blank page.
I found out the Problem is in lenght of the query i send... but how can i fix it???
My it be the Problem, if a query is longer then 6000 Characters?
what is the purpose of a temporary table like in this statement? how is it different than a regular table?
CREATE TEMPORARY TABLE tmptable
SELECT A.* FROM batchinfo_2009 AS A, calibration_2009 AS B
WHERE A.reporttime LIKE '%2010%'
AND A.rowid = B.rowid;
I am using sort field in order to sort records. My code is this
$query = "SELECT max(sort) FROM $this->table LIMIT 1;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$sort = $row[0]+1;
$query = "INSERT INTO "$this->table." VALUES ( '', '$ini', '$time', '$ip', '0', '$type', '$sort', '$title', '$image', '0' );";
$result = mysql_query($query) or die(mysql_error());
What is the error here?
Hi all,
How to search multiple values separated by commas.
ex:
table name : searchTest
id name keyword
1 trophy1 test1,test2,test3
2 trophy2 test2,test5
Points:
If i search for test2 both results trophy1 and trophy2 should be display.
If i search for trophy1 then trophy1 should be as result.
How to solve this issue.
thanks in advance
I have a table with anniversary dates. I want a query that returns me rows of anniversaries coming up in the next 10 days. For instance:
birthdate
---------
1965-10-10
1982-05-25
SELECT birthdate FROM Anniversaries WHERE mystical_magical_mumbo_jumbo <= 10
+------------+
| birthdate |
+------------+
| 1982-05-25 |
+------------+
1 row in set (0.01 sec)
I'd like to keep the query in the form x <= 10, because I'll use that number 10 in other parts of the query, and if I set it to a variable, I can change it once everywhere by changing the variable, and not have to re-write the query.
Hi,
I've got a table with the following columns:
ID, sysid, x, y, z, timereceived
ID is a unique number for each row.
sysid is an ID number for a specific device (about 100 different of these)
x, y and z is data received from the device. (totally random numbers)
timereceived is a timestamp for when the data was received.
I need a SQL query to show me the last inserted row for device a, device b, device c and so on.
I've been playing around with a lot of different Select statements, but never got anything that works. I manage to get unique rows by using group by, but the rest of the information is random (or at least it feels very random).
Anyone able to help me?
There could be hundreds of thousands records in this table.
I'm attempting to create an anti-bruteforcer for the login page on a website. Unfortunately, my query is not working as expected. I would like to test how many times an IP address has attempted to login, and also return the ID of the user for my next step in the login process. However, I'm having a problem with the query... for one thing, this would only return rows if it was the same user as they had been trying to login to before. I need it to be any user. Secondly, regardless of whether I use LEFT JOIN, RIGHT JOIN, INNER JOIN or JOIN, it will not return the user's ID unless there is a row for the user in login_attempts.
SELECT COUNT(`la`.`id`), `u`.`id`
FROM `users` AS `u` LEFT JOIN `login_attempts` AS `la`
ON `u`.`id` = `la`.`user_id`
WHERE `u`.`username` = 'admin' AND `la`.`ip_address` = '127.0.0.1' AND `la`.`timestamp` >= '1'
Hi all and thanks in advance.
I have a small problem which can not resolve, I have this table, and I want to sort by date and group it (but I only show 1 per idCAT)
| id | idcat | name | date |
| 1 | 3 | xx | 2011-01-02 |
| 2 | 4 | xf | 2011-01-02 |
| 3 | 3 | cd | 2011-01-01 |
| 4 | 1 | cg | 2011-01-04 |
| 5 | 4 | ce | 2011-01-06 |
would like to stay that way, try in a way but I can not
| 2 | 4 | xf | 2011-01-01 |
| 3 | 3 | cd | 2011-01-02 |
| 4 | 1 | cg | 2011-01-04 |
Hello!
I have this database table:
id | url
-----------------------------------------
1 | http://stackoverflow.com/
2 | http://www.google.com
3 | http://example.com/somepage
4 | https://another.net?id=88
5 | http://hello.org/index.php?hello=2
6 | http://google.com?q=hello+world
I need to search all fields, where URL belongs to a certain host.
For example, if I give the query 'google.com', it will return rows 2 and 6 (www is ignored).
I get the host using PHP parse_url() function.
How this SQL query would look like?
Hi I have a table that looks like this
id : productid : featureid
and would have the following data:
(1, 1, 16)
(2, 1, 21)
(3, 1, 25)
(4, 2, 16)
(5, 2, 21)
(6, 2, 27)
where featureid is a foreign key to another table.
I need to select products that have both featureids of 16 and 25, in which case productid 1 but not productid 2
Can someone show me an example of how to format this query.
I have a WCHAR:
WCHAR foo[200];
I want to copy values into it:
if (condition)
{
foo = L"bar";
}
else
{
foo = L"odp";
}
What is the best way to do this?
I have a master and slave database running on different nodes. The master DB is subjected to huge no. of inserts/updates. The master DB size is close to 6 GB, while the log files are now occupying a space of more than 120 GB. I am running out of disk space and need to get rid of the log files.
Will deleting the log files in anyway affect the slave DB ? Presently, the slave is just a couple of seconds behind the master.
Is there someplace where I can see what steps I need to follow to delete those files
eg.
1)Shut down the slave
2)Shut down the master
3)Delete the log files
4)Start the Master
5)Start the Slave
Do I need to inform the slave that the log files have been deleted ?? If yes, what is the way to do it ?
Any help would be appreciated.
Thanks
I have this: 2010-04-08T01:01:00Z
I want to remove the 'T' and everything behind it as well.
Also I would like to rewrite the date into this format: 08-04-2010
How can I do this the easiest way?
Thanks
this is my table
/* oefenreeks leerplan */
CREATE TABLE leerplan_oefenreeks
(
leerplan_oefenreeks_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
leerplan_id INT NOT NULL,
oefenreeks_id INT NOT NULL,
plaats INT NOT NULL
);
/* fk */
ALTER TABLE leerplan_oefenreeks ADD CONSTRAINT fk_leerp_oefenr_leerplan FOREIGN KEY(leerplan_id) REFERENCES leerplan (leerplan_id) ON DELETE CASCADE;
ALTER TABLE leerplan_oefenreeks ADD CONSTRAINT fk_leerp_oefenr_oefenreeks FOREIGN KEY(oefenreeks_id) REFERENCES oefenreeks (oefenreeks_id) ON DELETE CASCADE;
/* unique s *//*when I execute the nexline, my fk_leerp_oefenr_leerplan constraint vanishes/disappears*/
ALTER TABLE leerplan_oefenreeks ADD CONSTRAINT un_leerp_oefenr UNIQUE(leerplan_id, oefenreeks_id);
ALTER TABLE leerplan_oefenreeks ADD CONSTRAINT un_leerp_oefenr_plaats UNIQUE(leerplan_id, plaats);
when I go and check only 3 constraints exist (fk_leerp_oefenr_leerplan is gone)
I don't understand why this happens, plz tell me (if you need more info just ask ;)
I'm writing a program that is supposed to take in a list of names from the user, store them in an array, and then search through the list to check and see if the next name the user enters is part of the original list of names. The issue I'm having is that when I go to enter a list of names, it only saves the last name entered into the list. I've searched the web and this site for similar issues, but I can't seem to find anything that answers this issue specifically. Here is the part of code where I have problem
void initialize(char names[][],const int MAX_NAMES,const int MAX_NAMELENGTH)
{
int i,Number_entrys;
printf("How many names would you like to enter to the list?");
scanf("%d",&Number_entrys);
if(Number_entrys>MAX_NAMES){
printf("Please choose a smaller entry");
}else{
for (i=0; i<Number_entrys;i++){
scanf("%s",names);
}
}
printf("%s",names);
}
trying very hard to teach myself, so be gentle lol.
I have a project where I need to select only the users that answered to some questions in a certain way (based on a filter).
The filter table (filter) looks like this
question | answer
Q1 | A
Q2 | B
The user table (answers) looks like this
user | question | answer
1 | Q1 | A
1 | Q2 | D
2 | Q1 | A
2 | Q2 | B
How can I select from the user table only the user(s) that match the filter?
I tried
"SELECT user FROM answers WHERE (question = Q1 AND answer = A)
AND (question = Q2 AND answer = B)"
and it doesn't work -- I get an empty result. Thank you.