How does distinct work with the following table:
id | id2 | time
-------------------
1 | 5555 | 12
2 | 5555 | 12
3 | 5555 | 33
4 | 9999 | 44
5 | 9999 | 44
6 | 5555 | 33
select distinct * from table
How does distinct work with the following table:
id | id2 | time
-------------------
1 | 5555 | 12
2 | 5555 | 12
3 | 5555 | 33
4 | 9999 | 44
5 | 9999 | 44
6 | 5555 | 33
select distinct * from table
How can i update specific column in table1 only when table2.date is >= NOW(). I tried
UPDATE table1 JOIN table2 ON (table1.ownerid = table2.ownerid) SET table.test = 'disabled' WHERE FROM_UNIXTIME(table2.dateto) <= NOW();
But its seems to now work and no error at all
I am new to mysql. I have downloaded mySql installer 5.7.13 of 320.2mb size. While installing i get error Internal Error(error retrieving product version) the installer will now close. screenshot Please can you help me to fix this.
Thanks in advance!
Is it possible to use an UPDATE statement within the WHERE clause of a SELECT statement?
I would like to execute something like this but it doesn't work:
SELECT * FROM table1 WHERE col1 < (UPDATE table1 SET col2='test' WHERE id=1)
I use mysql_query in PHP.
Is there a command like F9 which goes to previous command to the next command or a way to view all commands used? Using the retrieve command in zeus to go back to the command that was just used, how would i go to forward after using the F9 when i've gotten to far. Instead of restarting my search.
I need to check a value before inserting. However for some reason I can't figure it out. Here's my code:
set @accID = (select id from table2);
IF @accID IS NOT NULL THEN
INSERT INTO task
(account_id)
VALUES (@accID);
END IF;
What is wrong with the code above as it shows invalid sql syntax error?
I have a column datedate format, with 15 min intervals and another column called datavalues with corresponding data. I want to roll up the data values to 1 hr. I am attaching a screen shot. . So my new column should have the aggregated value of "DATA_VALUES" column. and the datetime column should represent just hours but not 15 min. Please help, I have tried cast() to convert to time but not able to proceed further.
I have a table with columns
id
, q_id
, Question
, Answer
, Q_Date
I have made a query which concatenates multiple rows having same q_id
.
Here is the query:
select q_id, Question, Link, Q_Date,
GROUP_CONCAT(Answer SEPARATOR 'n') as Answer
from ask
group by q_id, Question, Link, Q_Date
What I want to do is order by
the id
column, but when I select id
column, it shows all the rows, UnConcatenated.
please help
Php Select Statement works with id(with unique values) as record selector but will not work if I use a different column(with unique values) as a selector
THIS WORKS
$Idart = "4";
$sql2 = "SELECT * FROM articles where id in ({$Idart})";
$results2 = $conn->query($sql2);
$row2 = $results2->fetch_assoc();
THIS DOES NOT WORK
$Idart = "5-6142-8906-6641";
$sql2 = "SELECT * FROM articles where IDStamp in ({$Idart})";
$results2 = $conn->query($sql2);
$row2 = $results2->fetch_assoc();
I've tried a variety of different things with MYSQL including deleting "id" column and making "IDStamp" the primary key. Any thoughts appreciated.
Trying to figure out how to get this JOIN to work properly. Been sitting here for about 30 minutes. Can someone help me out? I am trying to subtract one form the other, to see the difference between invoice quantity and inventory volume.
SELECT Invoice.NameOfItem, SUM(Inventory.Volume - Invoice.Quantity) As TotalNeeded
FROM Invoice
INNER JOIN Inventory
ON Invoice.NameOfItem=Inventory.NameOfItem
GROUP BY Invoice.NameOfItem;
The issue is the output is incorrect.
SELECT NameOfItem, SUM(Quantity) AS TotalNumberNeeded From Invoice GROUP BY NameOfItem
subtracted from
SELECT NameOfItem, SUM(Volume) AS TotalNumberNeeded From Inventory GROUP BY NameOfItem
Is = -112. The output is currently "992"
I'm running the below code using POSTGRESQL in Jetbrains. I'm trying to output the results in a neat 2 column table (QUARTER, RESULTS) INSIDE the console. When I run the below code, it comes back, but in separate tables, making it annoying to have to consolidate the results from each. Is there a way to get multiple results in the same table so I can copy and paste the results INSIDE the console? Thank you
I'm running
SELECT COUNT(DISTINCT CUSTOMER) as Q216 FROM(
SELECT *
FROM TABLE
WHERE CUSTOMER IN ( SELECT CUSTOMER
FROM temp_08.Unemployment
WHERE TRANSACTION_DATE > '3/31/2016'))
SELECT COUNT(DISTINCT CUSTOMER) as Q116 FROM(
SELECT *
FROM temp_08.COF
WHERE CUSTOMER IN ( SELECT CUSTOMER
FROM temp_08.Unemployment
WHERE TRANSACTION_DATE between '12/31/2015' and '3/31/2016'))
I am currently setting up a website that uses a mysql database having more than 4 crore rows of data. I am using one server for files and separate mysql server. Two servers are having 20 core, 64 gb ram. Meanwhile I could see while executing longest mysql queries my mysql server is using two cores maximum hence it is taking more than 38 seconds to execute my longest query. see the result below.
How can i configure my mysql server so that it uses all the 20 cores to handle the mysql ? How can I achieve this ? Mysql version using 5.6
Atop result below.
There is a small hello world Flask with visit statistics. I managed to count visits with redis, but I also have to add support storing date and number of visits in MySQL database. The current code is: I was trying to access previously created database. Currently I have no idea how to store such info in MySQL.
I was trying to access previously created database. Currently I have no idea how to store such info in MySQL.
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host="redis")
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'passwd'
app.config['MYSQL_DATABASE_DB'] = 'hello'
app.config['MYSQL_DATABASE_HOST'] = 'mysql'
mysql.init_app(app)
@app.route("/")
def hello():
visits = redis.incr('counter')
html = "<h3>Hello, world!</h3>"
"<b>Visits:</b> {visits}"
"<br/>"
return html.format(visits=visits)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
How this can be solved? I will be grateful for any suggestions.
I am trying to write a query that will return counted records from the time they were created. The primary key is a particular house which is unique. Another variable is bidder. The house to bidder relationship is 1:1 but there can be multiple records for each bidder (different houses). Another variable is a count (CASE) of results of previous bids that were won. I want to be able to set the count to return the number of previous bids won at the time each house record was created. Currently, my query logs the overall number of previous bids won regardless of the time the house record was created. Any help would be great! Example:
SELECT h.house_id,
h.bidder_id,
h.created_date,
b.bids_won
FROM house h
LEFT JOIN bid_transactions b
ON h.house_id = b.house_id
LEFT JOIN (
SELECT bidder_id,
COUNT(CASE WHEN created_date IS NOT NULL AND transaction_kind = 'Successful Bid' THEN 1 END) bids_won
FROM bid_transactions
GROUP BY user_id
) b
ON h.bidder_id = b.bidder_id
ORDER BY j.created_date DESC
I'm trying to execute on an alter table to assign a column to a sequence nextval
for an auto-increment, but can't seem to figure out how to do this last part. The sequence is created fine, owners are all asigned, and table_a.id
is the owner of table_a_id_seq
. I've already created table_a_id_seq
.
In a postgres sql function, how do I format this correctly.
I've tried:
EXECUTE format('ALTER TABLE ONLY %s ALTER COLUMN id SET DEFAULT nextval($1::regclass)', new_table_name) USING new_seq_name;
But it says that $1 is not pointing to new_table_seq_name
.
I've also tried:
EXECUTE format('ALTER TABLE ONLY %s ALTER COLUMN id SET DEFAULT nextval("%s"::regclass)', new_table_name, new_seq_name);
But it tells me the sequence doesn't exist which makes me wonder if it needs to be behind a transaction separated by this statement.
How can I successfully execute this alter on new_table_name
? Thanks for the help!
I am trying to use inner both GROUP BY and ORDER BY command in a same query with INNER JOIN, its is not happening.
i have Employee table:
+------------+------+
| id | fname | lname| |
+------------+------+
| 11 | ABCD | XHME |
| 12 | CDEF | LMOP |
| 13 | MNOP | DDDD |
+---------+---------+
emp_details table
+----+--------+-----------+--------------+--------------+-------+
| id | emp_id | company | joining_date | Leaving_date | salary|
+---------+---------+--------------------+--------------|-------+
| 1 | 11 | Company 1 | 1999-01-03 | 2001-07-08 | 12000 |
| 2 | 11 | Company 2 | 2005-07-09 | 2007-01-31 | 16000 |
| 3 | 11 | Company 3 | 2002-04-07 | 2015-04-28 | 23000 |
| 4 | 12 | Companyxyz| 2000-10-12 | 2004-03-09 | 17000 |
| 5 | 12 | TestCom | 2010-10-10 | 2014-10-10 | 35000 |
+---+---------+-----------+--------------+--------------+-------+
I want to display like this
+------------+--------------+------------------+
| User Name | Last Company | Last Drawn Salary|
+------------+--------------+------------------+
| ABCD XMHE | Company 3 | 23000 |
| LDEF LMOP | TestCom | 35000 |
+------------+--------------+------------------+
My query is like this
SELECT employee.id AS eid, employee.employer_id, employee.fname, employee.lname, emp_details.id as emid, emp_details.emp_id, emp_details.company, emp_details.joining_date, emp_details.leaving_date, emp_details.last_drawn_salary
FROM employee
INNER JOIN emp_details ON employee.id = emp_details.emp_id
WHERE employee.pan='".$pan."'
GROUP BY emp_details.emp_id
ORDER BY emp_details.id DESC
I also tried using like this max(emp_details.id) as emid
but is still shows the 1st inserted column only. whats the problem here?
This question already has an answer here:
I have searched an all answers I found I can't get to work.
I am trying to create a form that adds a row to my database.
add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Street Form</title>
</head>
<body>
<form onSubmit="php/insert.php" method="post">
<p>
<label for="streetname">Street Name:</label>
<input type="text" name="streetname" id="streetname">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>
insert.php
<?php
include 'php/connect.php
// attempt insert query execution
$sql = "INSERT INTO streets (name) VALUES ($_POST['streetname'])";
mysql_query($sql);
// close connection
mysqli_close($link);
?>
I know my connect.php works because I used it to display a random row from my DB
I'm still VERY new to SQL so I know it's a simple fix but not sure where.
I'd love a form that could add 5 values at once from 5 input fields but that is further down the road
I've been reading articles about SQL Injection, and decided to modify my code to prevent SQL injection.
For example, I have an input which I insert the value to my database. Initially, my guard against injection was this:
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
// $data = addslashes($data);
$data = mysql_real_escape_string($data);
return $data;
}
$artist = $_POST["artist"]; // can be anything
$artist = test_input($artist); // escaped chars are &, quotes, <, >, n, r, etc.
if ($mysqli->query("SELECT * FROM `my_table` WHERE `artist` = '$artist'")->num_rows == 0) {
$mysqli->query("INSERT INTO my_table (artist) VALUES ('$artist')");
echo "New artist is added.";
} else {
echo "Artist already exists.";
}
In the articles I've read, it was suggested that one should use prepared statements. I've changed my code and used that:
$artist = $_POST["artist"]; // can be anything
$query = $mysqli->prepare("SELECT * FROM my_table WHERE artist = ?");
$query->bind_param("s", $artist);
$query->execute();
$result = $query->get_result();
$query->close();
if ($result->num_rows == 0) {
echo "Artist doesn't exist in the DB." . PHP_EOL;
$query = $mysqli->prepare("INSERT INTO my_table (artist) VALUES (?)");
$query->bind_param("s", $artist);
$query->execute();
if ($query->affected_rows > 0) {
echo "Artist is added to the DB." . PHP_EOL;
}
$query->close();
} else {
echo "Artist already exists in the DB." . PHP_EOL;
}
While this prevents SQL injection, it doesn't do anything about XSS. So I decided to modify test_input
(removed $data = mysql_real_escape_string($data);
) and use it to prevent script injection.
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$artist = $_POST["artist"]; // can be anything
$artist = test_input($artist);
Now, my problem is about using prepared statements. I'll be inserting three items; artist, album, and song. Repeating the same process (prepare, bind, execute, close) over and over again seems redundent to me. I want to create a function and wrap the prepared statement process with it. Something like this:
function p_statement($mysqli, $query_string = "", $type = "", $vars = []) {
$query = $mysqli->prepare($query_string);
$query->bind_param($type, $vars);
$query->execute();
$result = null;
preg_match("/^[A-Z]+/", $query_string, $command);
switch ($command[0]) {
case "SELECT":
$result = $query->get_result();
case "INSERT":
$result = $query->affected_rows;
}
$query->close();
return $result;
}
Though, this presents a problem: $vars
array. Since the number of variables that'll be passed to mysqli_stmt::bind_param()
will be variable/dynamic, I've used an array in the main function p_statement
. I don't know how I should the pass the items in the array to the mysqli_stmt::bind_param()
. bind_param
expects (type, var1, var2, varn,)
, and I've got an array.
How can I make this work?
I have the sample following numbers which are stored in an mysql db in the decimal(10,2) format
1499.3927125506 - 1499.39 -> this is how it saved into the database
384.41295546559 - 384.41
278.74493927126 - 278.74
537.44939271255 - 537.45
The actual total before saving into the database is 1700, however after the saving the total becomes 1699.99
How can I make the total 1700 NOT 1699.99?
I have imported a csv table into sql db using phpmyadmin. I guess the default format is decimal(8,5), or at least that is how it comes out. Seems overkill and I thought I could reduce to 4,1. Problem is there are around 470 fields. I know how to change one at a time but this would take a long time. Is there a faster way?
I am looking to pivot a data set result out but there is no aggregate that is happening.
Select StufferID from from #temp_Stuffers where SponsorID IN (111,222,333)
This is going to give me 0 - 2 results. How can I use pivot to make it
Sponsor ID StufferID1 StufferID2
111 S1 S2
222 S5
333
Rather than
SponsorID StufferID
111 S1
111 S2
222 S5
I have a column called start time which is populated with a number like "1465815600000" and increasing. This column is the number of milliseconds from the year 1970 jan 1st 12.00:00:000 AM
to a certain date in june 2016
. This is in integer, which I need to convert it to date time format.
EX: 1465815600000 => 2016-06-12-12:00:00 (something like this)
Can somebody help me with to write this function?
I have a problem when I try to update data in MySQL database.
There is no syntax error or something like that.
The code is totally correct.
<?php
$id = $_REQUEST['id'];
$newname = $_REQUEST['newname'];
$newemail = $_REQUEST['newemail'];
$newpassword = $_REQUEST['newpassword'];
mysql_connect('localhost','root','root') or die('Connectuion failed');
mysql_select_db('test');
mysql_query("UPDATE users SET name='$newname', email='$newemail', password='$newpassword' WHERE id='$id'");
echo "<h1>Your acount have been updated succefully</h1>";
mysql_close();
?>
To insert something to my mysql table i have to use this sql command:
INSERT INTO `votes` (`voteId`, `vote`, `pageId`, `userId`) VALUES (NULL, '5', '1', '1');
Right now I have about 30000 pageId
. Let's say I would like to add to every single pageId
from 5 to 25 votes. Every vote will be 1 or 5
. But there would be from 50% to 100% change for vote of 5
.
Is it possible to do? I tried to first use text spinner
to generate votes from 1 to 5 but it is not efficient to do with such a big about of data.
The year is 2009 and SQL Server does not have CREATE OR ALTER/REPLACE. This is what I do instead.
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'SynchronizeRemoteCatalog' AND ROUTINE_SCHEMA = 'dbo' AND ROUTINE_TYPE = 'PROCEDURE')
EXEC ('DROP PROCEDURE dbo.SynchronizeRemoteCatalog')
CREATE PROCEDURE dbo.SynchronizeRemoteCatalog
AS BEGIN
-- body
END
For triggers, you have to lean on the proprietary system views.
Is this the most accepted convention in the meantime?
EDIT: As n8wrl suggested, the official word suggests that this feature is not a high priority. Hence the question.
Long story short i need to return a varchar value if stored value is null but eithers bring me a NULL value or varchar value here is the code.
in the table i have some fields null and some with data
CASE WHEN DS.DBIRTH+DS.MBIRTH+DS.YBIRTH =
'' THEN CONVERT(VARCHAR(10),PT.CLIENTDBIRTHDATE,111)
ELSE 'INPUT DATE OF BIRTH'
CASE WHEN PT.CLIENTBIRTHDATE IS NULL THEN ''
ELSE '1800-01-01'
END
END AS BFIELD
this brings me something like this
NULL
1917/05/02
NULL
1923/02/02
1967/01/05
NULL
but i need something like this
01/01/1800
1917/05/02
01/01/1800
1923/02/02
1967/01/05
01/01/1800
sorry for the ultra noob question
I've got two tables:
login_log :: ip | etc.
ip_location :: ip | location | hostname | etc.
I want to get every IP address from login_log
which doesn't have a row in ip_location
.
I tried this query but it doesn't work:
SELECT login_log.ip
FROM login_log
WHERE NOT EXIST (SELECT ip_location.ip
FROM ip_location
WHERE login_log.ip = ip_location.ip)
ERROR: syntax error at or near "SELECT" LINE 3: WHERE NOT EXIST (SELECT ip_location.ip`
I'm also wondering if this query (with adjustments to make it work) is the best performing query for this purpose.
I am trying to count the number of times a specific filter is used given its number of occurrences in a URL parameter. I have the URL parameter as a column. Here is a sample row result:
"lat"=>"28.5383355", "lng"=>"-81.37923649999999", "near"=>"Orlando, FL", "end_period"=>"05/08/2016", "place_input"=>"Orlando, Florida", "capacity_max"=>"100", "capacity_min"=>"7", "package_type"=>"bareboat", "start_period"=>"05/08/2016"
How would I go about counting the number of occurrences of e.g "lat"
? I tried using a wildcard, but SQL returned the following error message:
operator does not exist: hstore ~~ unknown
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 50
Hope somebody is able to help me out.
Subject pretty much says it all. I have as specific table that PMA just will absolutely not let me edit the rows. I cannot use the inline table editor and and I cannot click the "edit" link for any row.
PMA barfs with a bad query error due to the fact that for one, the "where_clause" parameter is empty in the link code, thus causing it to run some query that looks like this: SELECT * FROM database
.table
WHERE .... WHERE WHAT?
I have no clue why PMA does not create the edit links correctly. There is most certainly and id column that is unique and set as primary.
I'm using version 4.5.4 on a windows easyphp16.1 set up.
I have tried analyzing, checking, repairing the table with no results.
Does anyone have any idea what the heck would be causing something like this?
i use chart.js to visualize data from mysql database, i read data from mysql database using php, i use json_encode to echo the results i want to display from the sql queries and then recieve it using ajax that all worked well for me but the problem is when i come to visualize the data i got no height for the y axis value for example if i have labels ("hi","hello","what") on the x axis coming from database and on y-axis i got values ("2.6","1.5","100") and also retrieved from the database i came with only output of three points on the x-axis regardless of the value of y-axis "i mean even the value 100 is on the x axis " and i have no lines connecting the three points but when i stop by the mouse on one of the points i got information about the point coordinates thnx in advance output result
I have the following SQL (I have removed some of the selesct fi:
SELECT node_revisions.title AS 'Task',
node_revisions.body AS 'Description',
Date_format(field_due_date_value, '%e/%c/%Y') AS 'Due Date',
users.name AS 'User Name',
(SELECT GROUP_CONCAT(Concat(CHAR(10),Concat_ws( ' - ', name, From_unixtime( TIMESTAMP,
'%e/%c/%Y' )),CHAR(10),COMMENT))
FROM comments
WHERE comments.nid = content_type_task.nid) AS 'Comments'
FROM content_type_task
INNER JOIN users
ON content_type_task.field_assigned_to_uid = users.uid
INNER JOIN node_revisions
ON content_type_task.vid = node_revisions.vid
ORDER BY content_type_task.nid DESC
This pulls back all my tasks and all comments associated with a task. The problem I am having is that the comments field; created using the *GROUP_CONCAT*, is truncating the output. I don't know why and I don't know how to overcome this. (It looks to be at 341ish chars)
This question already has an answer here:
I am just trying to print a few results for testing purposes but it generates the complete code instead. I am not sure how to fix it.
<?php
include "config.php";
function getfinalsp($gid, $lv) {
$ids = mysql_query("select * from actstatus where regid = '$gid'");
$row = mysql_fetch_array($ids);
$val = $row[$lv];
$fv = $row['regid'];
if($fv == $gid && $val != 'Yes') {
$gs = mysql_query("select * from register where username = '$gid'");
$grow = mysql_fetch_array($gs);
$gval = $grow['sid'];
getfinalsp($gval, $lv);
}
return $val;
}
$sp = "sam";
$vl = "123";
$act = getfinalsp($sp, $vl);
echo $act;
?>
In the above code I passed $sp
, $sl
variables for mysql query but I get this error:
Notice: Undefined index: 123 in C:xampphtdocsabcsam2.php on line 16
So here is my current Database Project. General idea of relations:
For additional info for last 3 mentioned tables (Users_Games, Games_Points, Games_Points_Users) I am using composite keys as primary key.
I have feeling that this design logic is flawed. Do I need to normalize my database a little more? Will I be able to make those relations work with Eloquent? Maybe I should create additional table Players which will be subtype of Users?
I have two tables of time series data that I am trying to query and don't know how to properly do it.
The first table is time series data of device measurements. Each device is associated with a source and the data contains an hourly measurement. In this example there are 5 devices (101-105) with data for 5 days (June 1-5).
device_id date_time source_id meas
101 2016-06-01 00:00 ABC 105
101 2016-06-01 01:00 ABC 102
101 2016-06-01 02:00 ABC 103
...
101 2016-06-05 23:00 ABC 107
102 2016-06-01 00:00 XYZ 102
...
105 2016-06-05 23:00 XYZ 104
The second table is time series data of source measurements. Each source has three hourly measurements (meas_1, meas_2 and meas_3).
source_id date_time meas_1 meas_2 meas_3
ABC 2016-06-01 00:00 100 101 102
ABC 2016-06-01 01:00 99 100 105
ABC 2016-06-01 02:00 104 108 109
...
ABC 2016-06-05 23:00 102 109 102
XYZ 2016-06-01 00:00 105 106 103
...
XYZ 2016-06-05 23:00 103 105 101
I am looking for a query to get the data for a specified date range that grabs the device's measurements and its associated source's measurements. This example is the result for querying for device 101 from June 2-4.
device_id date_time source_id d.meas s.meas_1 s.meas_2 s.meas_3
101 2016-06-02 00:00 ABC 105 100 101 102
101 2016-06-02 01:00 ABC 102 99 100 105
101 2016-06-02 02:00 ABC 103 104 108 109
...
101 2016-06-04 23:00 ABC 107 102 109 102
The actual data set could get large with lets say 100,000 devices and 90 days of hourly measurements. So any help on properly indexing the tables would be appreciated. I'm using MySQL.
Assume I have the following tables/fields:
CREATE TABLE tbl_projects (
prjc_id int PRIMARY KEY
)
CREATE TABLE tbl_project_requirements (
preq_prjc_id int -- Foreign key to tbl_projects
preq_type_id int -- A standardized requirement category
)
Given a specific project, I would like to find other projects that have nearly similar requirement categories... or let's say at least a 75% overlap on their requirements.
I could do the following:
DECLARE @prjc_id int = 1
CREATE TABLE #project_reqs (req_type_id int)
INSERT INTO #project_reqs
SELECT preq_req_type_id
FROM tbl_project_requirements
WHERE preq_prjc_id = @prjc_id
SELECT prjc_id
FROM tbl_projects
CROSS APPLY (
SELECT CASE
WHEN COUNT(*) = 0 THEN 0.0
ELSE COALESCE(SUM(CASE WHEN type_id = prjc_type_id THEN 1.0 ELSE 0.0 END), 0.0)
/ CONVERT(float, COUNT(*))
END AS similarity
FROM #project_reqs
FULL OUTER JOIN tbl_project_requirements
ON preq_type_id = type_id
WHERE preq_prjc_id = prjc_id
) reqs
WHERE prjc_id != @prjc_id
AND similarity >= 0.75
In the above, I'm dividing the matched requirement categories by the total distinct requirement categories between each two projects to get the % overlap.
While this works, I sense code smells, and don't think this will scale very well. Is there any sort of method that exists to performantly calculate overlap of child records between two items? Maybe some sort of partial hash matching or...?
This is a bare bones strip of my actual code, but I would like to undestand how to do this so I can apply it throughout. I have code like this:
declare
use_t3 varchar2(1): = 'N'; //Y or N
use_t4 varchar2(1): = 'N'; //Y or N
begin
INSERT INTO MY_BIG_TABLE
SELECT
t3.qid AS "QID",
(SELECT t5.flag FROM t5 WHERE(t5.qid = t4.qid)) AS "FLAG"
FROM
t1 p,
t2 pm,
t3 pms,
t4 pmo
WHERE
t1.id = t2.qid
AND t3.day = 'Monday'
AND t2.id = t4.pmd_id(+)
AND t4.date IS NULL
end;
Now I have two variables:
use_t3
use_t4
If use_t3 and use_t4 are both equal to 'Y' (which means Yes, use these tables) then the query should be run as is shown exactly as both tables should be used (only either use_t1 or use_t2 can be equal to 'N', which means there will always be one which is qual to 'Y').
If use_t3 = 'N' and use_t4 = 'Y' then I would like the t3.qid column to still show but to return null, and for the condition:
t3.day = 'Monday'
to not be used.
If use_t3 = 'Y' and use_t4 = 'N' then I would like the "FLAG" column to return blank values, and the conditions:
t2.id = t4.pmd_id(+)
AND t4.date IS NULL
to not be used.
I hope this makes sense.
Cheers
I've created a view which uses GROUP_CONCAT to concatenate results from a query on products column with data type of 'varchar(7) utf8_general_ci'
in a column named concat_products
.
The problem is that mysql truncates value of concat_products column.
phpMyAdmin says the data type of concat_products column is varchar(341) utf8_bin
table products:
CREATE TABLE `products`(
`productId` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
`product` varchar(7) COLLATE utf8_general_ci NOT NULL,
`price` mediumint(5) unsigned NOT NULL,
PRIMARY KEY (`productId`))
ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
concat_products_vw View:
CREATE VIEW concat_products_vw AS
SELECT
`userId`,
GROUP_CONCAT(CONCAT_WS('_', `product`, `productId`, `price`)
ORDER BY `productId` ASC SEPARATOR '*') AS concat_products
FROM
`users`
LEFT JOIN `products`
ON `users`.`accountBalance` >= `product`.`price`
GROUP BY `productId`
according to mysql manual
Values in VARCHAR columns are variable-length strings
Length can be specified as a value from 1 to 255 before MySQL 4.0.2 and 0 to 255 as of MySQL 4.0.2.
edit:
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535.
Why mysql specifies more than 255 characters for varchar concat_products column?(solved!)
Why uf8_bin instead of utf8_general_ci?
Is it possible to change the data type of a column in a view for example in my case to text for concat_products column?
If not what can i do to prevent mysql from truncating concat_products column?
I have a SQL database with approximately 9 million observations, divided into 5 tables. I have established ODBC connectivity in R using RODBC and have explored the data a bit. The end-goal of what I am trying to do is to create a database that people with their own research questions related to the data can query the database and extract relevant variables to perform their analyses. To do this, I first need to create a matrix of missingness to figure out what variables exist for which observations. The tables cover the time period from 1994-2012, though 2 tables only cover up to 2011. I envision the matrix looking something like this(see link below). I think making a matrix of missingness for each year, with each table in the database as a row, then every variable in the database as the columns, and in each cell would be the number of observations and % missing. I am open to other ideas of course. I am not sure how to go about making this happen, however, as there are different columns in each table and there are also some columns that are the same (i.e. id, name, etc). I am wondering if it is best to create such a matrix for each table first, with each year as a row, then the variables as columns, and cells containing the same information as in the end-goal matrix. I would then make this a permanent table to be manipulated using R alone. This would prevent my machine from crashing/taking forever by joining all of those observations into a massive table. Then I would still need to unite all the tables by year. Any suggestions/insight/feedback are welcomed. Thank you all in advance.
Individual year of matrix of missingness:
I have the following Ruby on Rails entity:
Playlist:
class Playlist < ActiveRecord::Base {
:id => :integer,
:name => :string,
:created_at => :datetime,
:updated_at => :datetime,
:dimension => :string,
:title => :string,
:text => :string,
:price => :float,
:playlist_image_file_name => :string,
:playlist_image_content_type => :string,
:playlist_image_file_size => :integer,
:playlist_image_updated_at => :datetime,
:main => :boolean,
:action => :string,
:hairdresser_id => :integer
}
And Keyword:
class Keyword < ActiveRecord::Base {
:id => :integer,
:name => :string,
:created_at => :datetime,
:updated_at => :datetime,
:preselected => :boolean
}
The relation between them is very simple: basically a Playlist object can have 0 or more keywords associated. Those are the models:
class Keyword < ActiveRecord::Base
has_and_belongs_to_many :playlists
end
class Playlist < ActiveRecord::Base
has_and_belongs_to_many :keywords
has_attached_file :playlist_image, styles: {medium: "500x500", small: "200x200", thumb: "40x40"}, default_url: "/system/playlist_default.jpg"
validates_attachment_content_type :playlist_image, content_type: /Aimage/.*Z/
end
That allows me to do something like that:
Playlist.first.keywords
and retrieve all the keywords associated with the first playlist.
Now I would like to build a function to return all the playlists that have certain words as keywords and have "main" equals to true. For example all the playlist that have the keyword "Summer". I tryed with that:
Playlist.where(:main => true).map{|x| x.keywords.include? "Summer"}
But that returns only an array containing true or false depending if the related Playlist contains or not the keyword "Summer", I'm looking for something that return the whole playlist only if the array of keywords of that playlist include the word "summer". How can I achieve that?
I'm trying to return the row with ID 258776, here's what I've tried so far...
Without GROUP BY:
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at DESC
) AS main #GROUP BY main.uid
= ID = =MSG = =INB = = UID = = BD = = CREATED =
258776 258776 524785 570cc3a568402 wtf 2016-06-22 11:34:29
217149 217149 438907 570cc3a568402 <br /> 2016-04-12 11:45:09
Try with GROUP BY
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at DESC
) AS main GROUP BY main.uid
= ID = =MSG = =INB = = UID = = BD = = CREATED =
217149 217149 438907 570cc3a568402 <br /> 2016-04-12 11:45:09
Switch to ASC but GROUP BY gives same results ?
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at ASC
) AS main GROUP BY main.uid
= ID = =MSG = =INB = = UID = = BD = = CREATED =
217149 217149 438907 570cc3a568402 <br /> 2016-04-12 11:45:09
I presume it should work fine if I didn't need to use an INNER join ? :(
I have always had trouble getting SUMs on join tables, there is always an issue, I can get the results I need by running two queries, I am wondering if this two queries can be combine to make one join query, here is the queries I have and my attempt to join the query
Query 1
SELECT last_name, first_name, DATE_FORMAT( (mil_date), '%m/%d/%y' ) AS dates,
SUM( drive_time ) MINUTES FROM bhds_mileage LEFT JOIN bhds_teachers i
ON i.ds_id = bhds_mileage.ds_id
WHERE mil_date BETWEEN '2016-04-11' AND '2016-04-30'
AND bhds_mileage.ds_id =5
GROUP BY CONCAT( YEAR( mil_date ) , '/', WEEK( mil_date ) ) ,
bhds_mileage.ds_id
ORDER BY last_name ASC , dates ASC
the output in minutes is 271, 281, 279
Query 2
SELECT last_name, first_name, DATE_FORMAT((tm_date), '%m/%d/%y') AS dates,
SUM(tm_hours) total FROM bhds_timecard LEFT JOIN bhds_teachers i
ON i.ds_id = bhds_timecard.ds_id
WHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5
GROUP BY CONCAT(YEAR(tm_date), '/', WEEK(tm_date)), bhds_timecard.ds_id
ORDER BY last_name ASC, dates ASC
The output here is 33.00, 36.00, 26.75
Now my attempt to join the query
SELECT last_name, first_name, DATE_FORMAT((tm_date), '%m/%d/%y') AS dates,
SUM(tm_hours) total, SUM( drive_time ) MINUTES FROM bhds_timecard
LEFT JOIN bhds_teachers i ON i.ds_id = bhds_timecard.ds_id
LEFT JOIN bhds_mileage ON DATE_FORMAT((bhds_timecard.tm_date), '%m/%d/%y') =
DATE_FORMAT((bhds_mileage.mil_date), '%m/%d/%y') AND bhds_timecard.ds_id = bhds_mileage.ds_id
WHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5
GROUP BY CONCAT(YEAR(tm_date), '/', WEEK(tm_date)), bhds_timecard.ds_id
parenthesis is what is expected
this outputs 1044 (271), 1086 (281), 1215 (279)
I am using MySQlDb to connect and populate my DB with a python script. Data from a BGP stream (dump) is going in to the DB. But when I try to execute(insert data) with SQL on line 65 in the bottom of the code, the DB is not affected, besides that the row does auto-increment on one of it’s fields. Is this a encoding issue? I am using utf-8 in python and utf-8, utf8_swedish_ci in MySQL. Code I am using:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from _pybgpstream import BGPStream, BGPRecord, BGPElem
from collections import defaultdict
import time
import datetime
import os
import MySQLdb
db = MySQLdb.connect(user="bgpstream", host="localhost", passwd="Bgpstream9", db="bgpstream_copy")
db_cursor = db.cursor()
# Create a new bgpstream instance and a reusable bgprecord instance
stream = BGPStream()
rec = BGPRecord()
# Consider Route Views origon only
collector_name = 'rrc11'
stream.add_filter('collector',collector_name) #maybe we want route-views4?
t_end = int(time.time()) #current time now
t_start = t_end-3600 #the time interval (duration) we are getting from collecor, e.i 60*60 = 3600s = 1 hour
stream.add_interval_filter(t_start,t_end)
print "Total duration " + str(t_end-t_start) + " sec"
# Start the stream
stream.start()
### Insert loop ###
# This loop insert new records and tries not to over count the records
# Get next record:
while(stream.get_next_record(rec)):
# Print the record information only if it is not a valid record
if rec.status != "valid":
print rec.project, rec.collector, rec.type, rec.time, rec.status
else:
# Skip if rib
if rec.type == "rib":
continue
#get affected rows from insert
affected_rows = db.affected_rows()
# Skip if dulpicate record
if affected_rows <= 0:
continue
# Extract insert id of last inserted bgp record
last_record_id = db.insert_id()
print last_record_id
# Traverse elements
elem = rec.get_next_elem()
while(elem):
print last_record_id
## Dette bør kaste en exeption
if elem == None:
continue
# Insert element
db_cursor.execute(
"""INSERT INTO bgp_elements
(record_id_owner, element_time, peer_address, peer_asn)
VALUES
(
'"""+str(last_record_id)+"""',
'"""+str(elem.time)+"""',
'"""+str(elem.peer_address)+"""',
'"""+str(elem.peer_asn)+"""'
)
""")
elem = rec.get_next_elem()
I have researched for quite a while on how to get this done through other questions, but I cannot quite get a hold of it. This link seemed most similar, but I could not implement it correctly, perhaps it can help someone on this question? MySQL while loop query inside other while loop query
I am looking to display a table that uses info from our DB to display a user's parlays, games within parlays and associated wagers and odds. I would like it to look like the following:
Parlay ID Home Team Away Team User Wager Odds
Team A Team B
1 Team C name of teams.id = 4 Ridge Robinson 1000 2.0
name of teams.id = 12 name of teams.id = 16
-------------------------------------------------------------------------------------- (<tr> border)
Team C name of teams.id = 4
2 name of teams.id = x name of teams.id = x
-------------------------------------------------------------------------------------- (<tr> border)
I can easily create this table showing all columns for each item, but the problem is, each item then becomes its own row. As you can see in my example, there is a parlay showing per row...that means there is only one parlay id, one wager, one user, one odds, etc per PARLAY. The only multiple event is the number of games per parlay that can vary between 1 or greater.
I created this table using a while loop to loop through each parlay, but unfortunately, this table shows information on each row for each column, rather than what I show in my picture.
while($row = $result->fetch_array()) {
$output = '<tr>';
$output .= '<td>'.$row['Parlay ID'].'</td>';
$output .= '<td>'.$row['Home Team'].'</td>';
$output .= '<td>'.$row['Away Team'].'</td>';
$output .= '<td>'.$row['User'].'</td>';
$output .= '<td>'.$row['Wager'].'</td>';
$output .= '<td>'.$row['Odds'].'</td>';
$output = '</tr>';
}
I believe that I will have to create some secondary loop inside the 'Home Team' and 'Away Team' $output's...does anyone know how I can get this to work where the loop will show all games in my PARLAYGAMES table that match the Parlay ID given by each row, in a way that can show it like my example table above?
I am thinking something similar to below. I know the format is terrible(!), but I am not sure how to write it correctly so I am trying to convey my idea/logic.
$output .= '<td>'while(*PARLAY TABLE id = $row['PARLAYGAMES.parlayid']) { echo PARLAYGAMES.gameid }'</td>';
For reference, my DB table has games listed as with the home and away teams a reference to the id of the teams table.
GAMES
id Home Team Away Team
1 10 5
I then have a
PARLAY TABLE
id userid wager odds
1 44 1000 2.0
Finally, I have a
PARLAYGAMES TABLE
id parlayid gameid
1 1 1
2 1 4
Thanks for any assistance!
EDIT
I worked to keep my example as simple as possible by not showing all table columns, etc so we can get the logic/code part down, but I am going to show my whole query as it seems to make the most sense to do so:
SELECT
u.first_name AS 'User First Name',
u.last_name AS 'User Last Name',
u.id AS 'User ID',
ht.name AS 'Home Team',
away.name AS 'Away Team',
p.wager AS 'Wager',
p.odds AS 'Odds',
pg.parlayid AS 'Parlay ID',
pg.betinfo AS 'Bet Info',
pg.gameid AS 'Game ID',
g.date AS 'Game Date',
b.betting_site_name AS 'Betting Site'
FROM parlays p
JOIN parlaygames pg ON p.id = pg.parlayid
JOIN games g ON pg.gameid = g.id
JOIN teams ht ON g.home_team = ht.id
JOIN teams away ON g.away_team = away.id
JOIN users u ON u.id = p.userid
JOIN bonuses b ON p.bettingsiteid = b.id
ORDER BY
pg.parlayid,
pg.gameid