Hi I'm trying to delete a row from mysql using the primary key. I've looked up a couple of examples and exactly followed the syntax, but mysql table is not getting affected.
I'm using:
DELETE FROM user_info WHERE username='t2';
mysql reponse:
Query OK, 0 rows affected (0.00 sec)
Screetshot of mysql table
seems you have single quote arount t2 in db 't2'
then try using
DELETE FROM user_info WHERE username="'t2'" ;
or
DELETE FROM user_info WHERE username like concat('%','t2', '%') ;
if you really have single quote saved in db arout t2 .. the you should avoid this kind of storage behaviour is really a bad practice store a value with (unuseful ) quote around
+----+------+------------+
| ID | NUM | CREATED |
+----+------+------------+
| 1 | 11 | 2018-01-01 |
| 2 | 22 | 2018-02-01 |
| 3 | 11 | 2018-03-01 |
| 4 | 44 | 2018-04-01 |
| 5 | 22 | 2018-05-01 |
| 6 | 44 | 2018-04-02 |
+----+------+------------+
DELETE from numbers where ID = 1;
+----+------+------------+
| ID | NUM | CREATED |
+----+------+------------+
| 2 | 22 | 2018-02-01 |
| 3 | 11 | 2018-03-01 |
| 4 | 44 | 2018-04-01 |
| 5 | 22 | 2018-05-01 |
| 6 | 44 | 2018-04-02 |
+----+------+------------+
Related
I read that the output of the subquery doesn't matter and only its existence matter. But, when I change the code in the subquery, why is my output changing?
These are the tables:
mysql> select * from boats;
+------+-----------+-------+
| bid | bname | color |
+------+-----------+-------+
| 101 | Interlake | blue |
| 102 | Interlake | red |
| 103 | Clipper | green |
| 104 | Marine | red |
+------+-----------+-------+
mysql> select * from sailors;
+------+---------+--------+------+
| sid | sname | rating | age |
+------+---------+--------+------+
| 22 | Dustin | 7 | 45 |
| 29 | Brutus | 1 | 33 |
| 31 | Lubber | 8 | 55.5 |
| 32 | Andy | 8 | 25.5 |
| 58 | Rusty | 10 | 35 |
| 64 | Horatio | 7 | 35 |
| 71 | Zorba | 10 | 16 |
| 74 | Horatio | 9 | 40 |
| 85 | Art | 3 | 25.5 |
| 95 | Bob | 3 | 63.5 |
+------+---------+--------+------+
10 rows in set (0.00 sec)
mysql> select * from reserves;
+------+------+------------+
| sid | bid | day |
+------+------+------------+
| 22 | 101 | 1998-10-10 |
| 22 | 102 | 1998-10-10 |
| 22 | 103 | 1998-10-08 |
| 22 | 104 | 1998-10-08 |
| 31 | 102 | 1998-11-10 |
| 31 | 103 | 1998-11-06 |
| 31 | 104 | 1998-11-12 |
| 64 | 101 | 1998-09-05 |
| 64 | 102 | 1998-09-08 |
| 74 | 103 | 1998-09-08 |
+------+------+------------+
select sname from sailors s where exists(select * from reserves r where r.bid=103);
+---------+
| sname |
+---------+
| Dustin |
| Brutus |
| Lubber |
| Andy |
| Rusty |
| Horatio |
| Zorba |
| Horatio |
| Art |
| Bob |
+---------+
10 rows in set (0.00 sec)
mysql> select sname from sailors s where exists(select * from reserves r where r.bid=103 and r.sid=s.sid);
+---------+
| sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
Also, I am not able to understand what r.sid=s.sid is doing here. All the sid in reserves are already from sailors table. Please someone explain it to me.
The EXISTS is a Boolean Operator which indicates that if there is ANY row in the sub-query you passed to it. When you execute this:
EXISTS(SELECT * FROM reserves r WHERE r.bid=103)
It will return TRUE after finding the FIRST row which has the condition bid = 103 in Reserves table. The first part of the query doesn't matter, it does not matter what you SELECT in Exists and MySQL engine will ignore it, just the WHERE clause is the part which makes the difference, you can use Exists even like this:
EXISTS(SELECT 1 FROM reserves r WHERE r.bid=103)
In the query above, nothing depends on the values in main query, nothing depends on Sailors table, and if there is ANY row in the Reserves table with bid = 103, then it always will return TRUE.
In the second sub-query with EXISTS, you have a different WHERE clause, and it depend on the value of the fields of the main Query, so it will have different result per each row:
EXISTS(SELECT * FROM reserves r WHERE r.bid=103 AND r.sid=s.sid)
In the above query, per each row in Sailors table, MySQL uses sid value to produce the WHERE condition of the sub-query in EXISTS operator, so it will returns TRUE for a row in Sailors table if there are ANY rows in Reserves table which has a bid = 103 and sid = Sailors.sid, and it will returns False for those that has not such a record in Reserves table, and finally you will get a different result
I think I got that. Exists is used to check if the subquery is existing for the main query. I didn't give any link for the main query and subquery in the first query.
For every name in sailors, independently, the subquery is existing. Hence, I got all the names. In the second query, I added s.sid=r.sid which links the main query and subquery. It checks if for a sname, if bid=103, and also, if s.sid=r.sid.
Please comment if I got that right.
How can I fetch the full row that contain the max(column).
Example:
I have a table like this
+----+---------------+------+---------------------+
| id | title | c | update |
+----+---------------+------+---------------------+
| 1 | John S. | 15 | 2017-09-15 09:04:13 |
| 1 | John S. | 21 | 2017-09-15 09:04:29 |
| 1 | John S. | 22 | 2017-09-16 01:55:26 |
| 43 | Cristover Co. | 15 | 2017-09-17 14:11:43 |
| 43 | PeterSberg R. | 16 | 2017-09-17 15:12:30 |
| 43 | Cristover Co. | 19 | 2017-09-17 21:45:10 |
+----+---------------+------+---------------------+
Now I want to select the row that contains the max(update). Expected result:
+----+---------------+------+---------------------+
| id | title | c | update |
+----+---------------+------+---------------------+
| 43 | Cristover Co. | 19 | 2017-09-17 21:45:10 |
+----+---------------+------+---------------------+
First, you shouldn't name your column update, choose another name, let's say "date_of_update"
If you want to select only the row with the most recent update, you can simply use something like this :
SELECT * FROM table_name ORDER BY date_of_update DESC LIMIT 1.
This will give you only one row with the max date!
try this one
SELECT * FROM table_name
WHERE table_name.update =
(SELECT max(table_name.update) from table_name)
I have a table that looks like this if I 'select *'
+----+--------+------+------------+
| id | name | task | day |
+----+--------+------+------------+
| 1 | Rodney | 2 | 2016-05-05 |
| 2 | Rodney | 2 | 2016-05-08 |
| 3 | Rodney | 8 | 2016-05-08 |
| 4 | Scott | 2 | 2016-05-05 |
| 5 | Scott | 8 | 2016-05-05 |
| 6 | Frank | 2 | 2016-05-05 |
| 7 | Frank | 2 | 2016-05-08 |
+----+--------+------+------------+
What I'm trying to achive is a query that will get the last entered 'task' for each person. So, in this case I would want back:
2 | Rodney | 2 | 2016-05-08
3 | Rodney | 8 | 2016-05-08
4 | Scott | 2 | 2016-05-05
5 | Scott | 8 | 2016-05-05
7 | Frank | 2 | 2016-05-08
I'm pretty sure I need to use distinct against name & task and max for the most recent entry. Just not sure how to structure the two of them together to get the result.
select distinct name, task from test;
Gets me close...
+--------+------+
| name | task |
+--------+------+
| Rodney | 2 |
| Rodney | 8 |
| Scott | 2 |
| Scott | 8 |
| Frank | 2 |
+--------+------+
But no date...My SQL is limited. Any help would be appreciated.
Aggregate your rows so as to get the latest day per name. Then access the table again to get the records matching thse days:
select *
from test
where (name, day) in
(
select name, max(day)
from test
group by name
);
Another way is to select the records for which not exists a later record for the same name:
select *
from test
where not exists
(
select *
from test later
where later.name = test.name
and later.day > test.day
);
I have a MySQL table with a column which I want to auto increment by +1 value for a specific number of rows (where id_sample='2'). What do you think, I can archive this by a single query, or I need to update row by row :(.
A short preview of my table is:
+----------+----------+---------+
| id | id_sample| degrees |
+----------+----------+---------+
| 361 | 2 | 0 |
| 362 | 2 | 0 |
| 363 | 2 | 0 |
| 364 | 2 | 0 |
| 365 | 2 | 0 |
| 366 | 2 | 0 |
| ... | .... | .... |
+----------+----------+---------+
I want to archive this:
+----------+----------+---------+
| id | id_sample| degrees |
+----------+----------+---------+
| 361 | 2 | 1 |
| 362 | 2 | 2 |
| 363 | 2 | 3 |
| 364 | 2 | 4 |
| 365 | 2 | 5 |
| 366 | 2 | 6 |
| ... | .... | .... |
+----------+----------+---------+
I tried this query (see bellow) but I got an increment for all rows: with the corresponding id_sample='2':
UPDATE myTable SET degrees=degrees+1 WHERE id_sample='2';
If you can use variables. Here is a solution:
SET #rownum=0;
UPDATE myTable SET degrees=(#rownum:=#rownum+1) WHERE id_sample='2';
Inspired by this: Display Rownum issue MySQL
Update mytable a set a.degrees = (select nvl(max(b.degree),0)+1 from mytable b where a.id=b.id and b.id_sample=2);
Say i have a table as shown:
id, auctionUser, auctionId, MinPrice, NumBids, PlacedBids
And then say i've got the following entries in above table that have the same auctionId:
1 | user1 | 99 | 10.25 | 20 | 0
2 | user2 | 99 | 10.50 | 50 | 0
Is there a way to write a query ( WHERE auctionId = 99 ) that would return a row for every 0.01 of MinPrice where the two rows would 'intersect' (don't know if that's the right word but it's the best i could come up with to describe it) based on the number of bids in NumBids? So for the data above, there would be an 'intersect' of the two users from 10.50 thru 10.75. I'd like to be able to create the flowing data to display like so , alternating bids between the users for the number of bids set in NumBids:
(bidAmount) | (auctionUser) | NumBids | PlacedBids
10.50 | user2 | 50 | 1
10.51 | user1 | 20 | 1
10.52 | user2 | 50 | 2
10.53 | user1 | 20 | 2
10.54 | user2 | 50 | 3
10.55 | user1 | 20 | 3
.
.
.
10.70 | user2 | 50 | 20
10.71 | user1 | 20 | 20 <-- ends here for user1 since 20 NumBids would be used up
10.72 | user2 | 50 | 21
I don't even know if this is possible via a sql query or not -- or even how to start such a query. I thought i'd throw it out there to see if any sql guru's had and idea. I figured if there was a way to do it, it would probably be much faster to produce it from a query that trying to use php to cycle through and produce the result...maybe not though.
As always, MUCHO THANKS for any time and advice you can spare on this!
I can't say I understand exactly what you want, but I think you need to generate rows. One way of generating rows is to use a Numbers table, which is basically a table of consecutive integers.
Have a look at my answer to this question. It is not related to your question, but there is code to generate such a numbers table.
So if you want to generate 1 row for each 0,01 difference, you would calculate nr of cents (or whatever the currency was) and join to the numbers table with a filter on n < nr_of_cents.
Edit:
Ok, I'll try. First, some sample data.
create table auctions(
auctionuser int
,auctionid int
,minprice decimal(5,2)
,numbids int
);
insert into auctions values(1, 1, 2.20, 2);
insert into auctions values(2, 1, 3.30, 4);
insert into auctions values(3, 1, 4.40, 6);
select *
from auctions
where auctionid = 1;
+-------------+-----------+----------+---------+
| auctionuser | auctionid | minprice | numbids |
+-------------+-----------+----------+---------+
| 1 | 1 | 2.20 | 2 |
| 2 | 1 | 3.30 | 4 |
| 3 | 1 | 4.40 | 6 |
+-------------+-----------+----------+---------+
3 rows in set (0.00 sec)
I think the following is close to what you want. Note that I have used the numbers table in the post I linked to.
select a.auctionuser
,n as user_bid
,minprice
,numbids
,a.minprice + (0.01 * (n-1)) as bid
from auctions a
,numbers
where numbers.n <= a.numbids
and a.auctionid = 1
order
by n
,a.minprice
,a.auctionuser;
+-------------+----------+----------+---------+------+
| auctionuser | user_bid | minprice | numbids | bid |
+-------------+----------+----------+---------+------+
| 1 | 1 | 2.20 | 2 | 2.20 |
| 2 | 1 | 3.30 | 4 | 3.30 |
| 3 | 1 | 4.40 | 6 | 4.40 |
| 1 | 2 | 2.20 | 2 | 2.21 |
| 2 | 2 | 3.30 | 4 | 3.31 |
| 3 | 2 | 4.40 | 6 | 4.41 |
| 2 | 3 | 3.30 | 4 | 3.32 |
| 3 | 3 | 4.40 | 6 | 4.42 |
| 2 | 4 | 3.30 | 4 | 3.33 |
| 3 | 4 | 4.40 | 6 | 4.43 |
| 3 | 5 | 4.40 | 6 | 4.44 |
| 3 | 6 | 4.40 | 6 | 4.45 |
+-------------+----------+----------+---------+------+
12 rows in set (0.00 sec)