I have a table like:
gold
gold_city | gold_type | gold_selltime
-------------------------------------
city1 | type 1 | 2012-01-01
city1 | type 1 | 2012-02-02
city1 | type 1 | 2012-03-03
city2 | type 2 | 2012-01-01
city2 | type 2 | 2012-02-02
city2 | type 2 | 2012-03-03
city3 | type 3 | 2012-01-01
city3 | type 3 | 2012-02-02
city3 | type 3 | 2012-03-03
How can I get 1 last result order by gold_selltime desc each group by gold_city and gold_type
I used this:
SELECT * , COUNT( * )
FROM gold_2012
GROUP BY gold_type , gold_city
ORDER BY gold_selltime DESC
but it did work.
I only have result like:
gold_city | gold_type | gold_selltime
-------------------------------------
city1 | type 1 | 2012-01-01
city2 | type 2 | 2012-01-01
city3 | type 3 | 2012-01-01
but I need it like:
gold_city | gold_type | gold_selltime
-------------------------------------
city1 | type 1 | 2012-03-03
city2 | type 2 | 2012-03-03
city3 | type 3 | 2012-03-03
I just wanted to develop a translation app in a Django projects which enables registered users with certain permissions to translate every single message it appears in latest version.
My question is, what character set should I use for database tables in this translation app? Looks like some european language characters cannot be stored in UTF-8?
I have set up my table with an index only on done_status(done_status =INT), when I use
EXPLAIN SELECT * FROM reminder WHERE done_status=2
i get this back
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE reminder ALL done_status NULL NULL NULL 5 Using where
but when I give this command
EXPLAIN SELECT * FROM reminder WHERE done_status=1
that's what I get back:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE reminder ref done_status done_status 4 const 2
first time it shows me it uses 5 rows second time 2 rows
I don't think the index works, if I understood it right first time it should give me 3 rows. What do I do wrong?
SHOW INDEX FROM reminder:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
reminder 1 done_status 1 done_status A 5 NULL NULL BTREE
I have the following two table scenario:
users
id groups
1 1,2,3
2 2,3
3 1,3
4 3
and
groups
id
1
2
3
How do I return the IDs of all users that belong to group 2 and 1 for example? Should I look into join, a helper group_membership table or function to separate the comma delimited group IDs to get something like this:
group_membership
user_id group_id
1 1
1 2
1 3
2 2
2 3
... ...
I have a table mytable( id, key, value). I realize that key is generating a lot of data redundancy since my key is a string. (my keys are really long, but repetititve) How do I build a separate table out that has (key, keyID) and then alternate my table to be mytable( id, keyID, value) and keyTable(keyID, key) ?
I have the following query....
UPDATE vehicle_catalog SET parent_id = 0 WHERE parent_id = SUBSTR(id, 0, 5)
I need to set all parent_ids to 0 where the first 5 characters of id is the same as the parent_id. This is effecting 0 rows when I'm looking at the data and it should be effecting over 10,000.
Any ideas on why this wouldn't be effecting all rows?
We are looking for Web Designers & Developers. Urgent Opening.
Profile:-
a) Have exp. in Designing websites in wordpress
b) Have Creativity in work
c) Send us your work – [email protected]
d) Min Exp. required: 2 + years
e) Can Integrate the Facebook, Twitter & other social networking websites.
To review our profile – please check – www.dicorporation.com & www.ismoip.com
I'm using PDO's bindParam.
This is the function which checks every GET variable on the website. After changing it will echo it out:
function Check_Get_Param($val){
$value1=addslashes($val);
$string1=htmlspecialchars($value1);
$string2=strip_tags($string1);
$string3=intval($string2);
return $string3;
}
Hhere this will output the result:
Check_Get_Param($_GET['id']);
Now the idea is any id or id= any or id = %
$_GET['id'] = % will result 0 as % is not integer. How to allow % also?
How do I modify this function or any other function that I could filter the GET parameters so I could keep out the web from injections?
I'm working on a custom forum system and I'm trying to figure out how to put a thread on the top of the list if a user posts in it.
I've got this for my query
SELECT
user_threads.threadID,
user_threads.title,
user_threads.uid,
user_threads.postDate,
thread_messages.posted
FROM
user_threads,
thread_messages
WHERE
parent = :parent
GROUP BY
user_threads.title
ORDER BY
thread_messages.posted
DESC
Which doesn't appear to be working. if I post in a new thread, it remains where it is on the list.
I'm having difficulty creating a month-count select query in SQL.
Basically, I have a list of entries, all of which have a date associated with them. What I want the end result to be, is a list containing 12 rows (one for each month), and each row would contain the month number (1 for January, 2 for February, etc), and a count of how many entries had that month set as it's date. Something like this:
Month - Count
1 - 12
2 - 0
3 - 7
4 - 0
5 - 9
6 - 0
I can get an result containing months that have a count of higher than 0, but if the month contains no entries, the row isn't created. I get this result just by doing
SELECT Month(goalDate) as monthNumber, count(*) as monthCount
FROM goalsList
WHERE Year(goalDate) = 2012
GROUP BY Month(goalDate)
ORDER BY monthNumber
Thanks in advance for the help!
I have an inventory system, where a User has many inventory. We have a barcode column which needs to be sequential for each user. I run into a problem however when doing bulk association building. I end up getting several inventories for a user with the same barcode.
For example:
Inventory Table:
id | user_id | barcode
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
5 | 1 | 3
In the Inventory model I have
before_validation :assign_barcode, on: :create
def assign_barcode
self.barcode = (user.inventories.order(barcode: :desc).first.try(:barcode) || 0) + 1
end
It generally works, but ran into a problem when seeding my db:
(1..5).each do
user.inventories.build(...)
end
user.save
I end up with a bunch of inventories for user that have the same barcode. How can I ensure that inventories have unique barcodes even when adding inventories in bulk?
I want to select a stored value from database and then put it into a temporary variable.
For example, I have a column called category, one value under it is m, so I want to select this m value from the database, let's say from a table of a database called user_info.
Then I want to put it into a variable, let's name it $res.
After that, I want to do some condition stuff, such as if $res=="m",
Can anyone help me write a simple structure here?
Here is the code:
<?php
$sql = "Select category FROM user_info WHERE user_name = '"
.$_SESSION['username']."' and password = '".$_SESSION['password']."'";
$res = mysql_query($sql);
if($res == "a"){
include('MPIncomeStrategy.php');
}
if($res == "b"){
include('MPIncomeStrategy.php');
}
But it seems that the code is not able to detect $res =="category value in database". Did I just use the wrong way to store the category value?
I have this SQL by a programmer:
$sql = "
INSERT INTO
`{$database}`.`table`
(
`my_id`,
`xType`,
`subType`,
`recordID`,
`textarea`
)
VALUES
(
{$my_id},
?xType,
?subType,
{$recordID},
?areaText
) ";
My question is why is he using ? before values? How do I see what values are coming in? I did echo and it shows ?xType as ?xType. No values. What does ? stand for in SQL?
Hello,
I am trying to increment a INT column by 1 if a certain field is not null on an update request, currently I have this update too columns,
public function updateCronDetails($transaction_reference, $flag, $log) {
$data = array (
'flag' => $flag,
'log' => "$log"
);
$this->db->where('transaction_reference', $transaction_reference);
$this->db->update('sy_cron', $data);
}
What I need to know is how I can check if the value being sent to the log field is NULL and if it is how could I increment a column called count by 1?
I am working on a basic messaging system. This is to get all the messages and to make the row of the table that has an unread message Green. In the table, there is a column called 'msgread'. this is set to '0' by default. Therefore it should make any row with the msgread = 0 - green. this is only working for the first row of the table with the code i have - i verified that it is always getting a 0 value, however it only works the first time through in the while statement ..
require('./connect.php');
$getmessages = "SELECT * FROM messages WHERE toperson = '" . $userid . "'";
echo $getmessages;
$messages = mysql_query($getmessages);
if(mysql_num_rows($messages) != 0) {
$table = "<table><tr><th>From</th><th>Subject</th><th>Message</th></tr>";
while($results = mysql_fetch_array($messages)) {
if(strlen($results[message]) < 30){
$message = $results[message];
}
else {
$message = substr($results[message], 0 ,30) . "...";
}
if($results[msgread] == 0){
$table .= "<tr style='background:#9CFFB6'>";
$table .= "<td>" . $results[from] . "</td><td>" . $results[subject] . "</td><td><a href='viewmessage.php?id=" . $results[message_id] ."'>" . $message . "</a></td></tr>";
}
else {
$table .= "<tr>";
$table .= "<td>" . $results[from] . "</td><td>" . $results[subject] . "</td><td><a href='viewmessage.php?id=" . $results[message_id] ."'>" . $message . "</a></td></tr>";
}
}
echo $table ."</table>";
}
else {
echo "No Messages Found";
}
There's all the code, including grabbing the info from the database. Thanks.
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 a table, links1, that has the columns headers CardID and AbilityID, that looks like this:
CardID | AbilityID
1001 | 1
1001 | 2
1001 | 3
1002 | 2
1002 | 3
1002 | 4
1003 | 3
1003 | 4
1003 | 5
What I want is to be able to return all the CardID that that have two specific AbilityID.
For example:
If I choose 1 and 2, it returns 1001.
If I choose 3 and 4, it returns 1002 and 1003.
Is it possible to do this with only one table, or will I need to create an identical table and do an INNER JOIN on those?
Hi, I am pretty new to web development and I want to create a search system on my webpage, but I dont have any idea how to?
This search system must be able to search everything that is displayed on the webpage.
Please help me.
If a do a query such as:
SELECT COUNT(*) as num FROM table WHERE x = 'y'
Will it always return a result, even when the query doesn't match any record? Or do i need to validate and make sure a row is returned as the result?
So apparently i've stumbled upon a coding error when trying to select the time from my database.
SELECT * FROM `videos` WHERE `added_time` > AddTime( CurTime(), '14400 hour' )
is the code, i'm trying to select all the videos posted 10 days (14400 hours) ago using the "added_time" format, because it worked for my previous coding but in this one it work work.
Shown below is a link to the image showing how my database structure for videos are shown.
http://i.imm.io/NURT.png
Edit: Previously i had this problem for retrieving and deleting bulletins posted 10 days ago, and this code worked, however this code apparently won't work when trying to retrieve the videos :/ I don't know why, they're using the same format.
See: http://i.imm.io/NUSW.png
Here is my code
<?php require_once 'connect.php';
$sql = "SELECT * FROM `db-pages`";
$result = $mysqli->query($sql) or die($mysqli->error.__LINE__);
while ($row = $result->fetch_assoc()) {
echo($row['pagetitle'].' - To edit this page <a href="editpage.php?id='.$row['id'].'">click here</a><br>');
}
}
?>
I've added a couple more rows to the Database and it's returning them all, apart from id=1 in the DB. Any idea why?