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 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?
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 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 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 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 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'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.
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 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?
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?
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?
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.
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
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?
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?
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?
So I have this code to pass items from database to my order table. When I'm echoing the session. The session variable contains something so there's no problem with that. But when I echo those variables under numrows, it only shows nothing. Is there something wrong?
<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
require("connect.php");
$UserID = $_SESSION['CustNum'];
$UserN = $_SESSION['UserName'];
$ProdGTotal = $_SESSION['ProdGTotal'];
$queryord = mysql_query("SELECT * FROM customer WHERE UserName = '$UserN'");
$numrows = mysql_num_rows($queryord);
if(numrows == 1){
$row = mysql_fetch_assoc($queryord)or die ('Unable to run query:'.mysql_error()); // fetch associated: get function from a query for a database
$dbstreet = $row['Street'];
$dhousenum = $row['HouseNum'];
$dbcnum = $row['CelNum'];
$dbarea = $row['Area'];
$dbbuilding = $row['Building'];
$dbcity = $row['City'];
$dbpnum = $row['PhoneNum'];
$dbfname = $row['FName'];
$dblname = $row['LName'];
}
else
die(mysql_error());
$query4=mysql_query("INSERT INTO orderdetails VALUES ('', '$UserID', Now(), '$dbhousenum', '$dbstreet', '$dbarea', '$dbbuilding', '$dbcity', '$dbfname', '$dblname', '$dbcnum', '$dbpnum', '$ProdGTotal')",$connect);
if ($query4){
header("location:index.php");
}
else
die(mysql_error());
?>
select a.userid,(select firstName from user where userid=NOTUSED.userid) as z,
(select max(login_time) from userLoginTime AS b where userid = a.user_id
GROUP BY b.user_id ORDER BY b.user_id) as y
From(SELECT DISTINCT a.user_id FROM user AS a
LEFT OUTER JOIN (SELECT (userid) FROM userlogintime
where serialid=15400012)AS b ON user.user_id = b.user_id
where a.Serialid=15400012 AND b.userid IS NULL) NOTUSED,
Relation r, user a where r.childuserid = NOTUSED.userid
and guarduserid = a.userid
A problem I recently ran into was that when trying to update a field in my database using this code would not work. I traced it back to having a % sign in the text being updated ($note, then $note_escaped)... Inserting it with sprintf worked fine though.
Should I not be using sprintf for updates, or should it be formed differently?
I did some searching but couldn't come up with anything.
$id = mysql_real_escape_string($id);
$note_escaped = mysql_real_escape_string($note);
$editedby = mysql_real_escape_string($author);
$editdate = mysql_real_escape_string($date);
//insert info from form into database
$query= sprintf("UPDATE notes_$suffix SET note='$note_escaped', editedby='$editedby', editdate='$editdate' WHERE id='$id' LIMIT 1");
Thanks much!
if the search form is blank, it should display an error that something should be entered by the user. it should only show those results which contain the keywords the user has entered in the search textbox.
however, if the user enters % or _ or +, it displays all results. how do i display an error when the user enters these wildcard characters?
my search php code:
$search_result = "";
$search_result = $_GET["q"];
$search_result = trim($search_result);
if ($search_result == "") {
echo "<p>Search Error</p><p>Please enter a search...</p>" ;
exit();
}
$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . mysql_real_escape_string($search_result) .'%" ORDER BY idQuotes DESC', $conn)
or die ('Error: '.mysql_error());
// there's either one or zero records. Again, no need for a while loop
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES);
}
?>
<div class="caption">Search Results</div>
<div class="center_div">
<table>
<?php while ($row= mysql_fetch_array($result)) { ?>
<tr>
<td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
<td style="font-size:16px;"><?php h($cQuotes); ?></td>
<td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
<td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
</tr>
<?php } ?>
</table>
<?php
?>
</div>
Dear all,
I want to use index on table where i m searching a string using regex '^searchkeywordname$|searchkeywordname' ,this is scanning whole table .how can i retrieve fast result using index or sumthing