I recently imported an excel arch into an sql database. The values imported look like:
name age adress date
carl 12 something 2015-11-10
lisa 51 something2 2+15-10-09
steven 32 something3 2014-12-29
I then added an auto increment column named id, which resulted in:
id name age adress date
1 carl 12 something 2015-11-10
2 lisa 51 something2 2+15-10-09
3 steven 32 something3 2014-12-29
My problem is that I need to reverse the ids. Because if I now where to insert a new row, it'd result in the date column not matching up with the id column. It'd look like this:
id name age adress date
1 carl 12 something 2015-11-10
2 lisa 51 something2 2015-10-09
3 steven 32 something3 2014-12-29
4 neil 25 something4 2016-01-12
I've searched for methods of creating a new id column, reversed. Or reversing my existing column. Problem is I haven't succeeded very well.
Create a new empty table also with autoincremt and insert the rows in reverse order
INSERT into new_table
select '',name,age address,`date`
FROM org_table
ORDER BY id DESC;
Old topic but if anyone else is looking for an answer, here's what I found to do this:
SELECT MAX(id) as max_id FROM table;
Get this max_id value (example: 28542)
Ensure you that your "id" column is not UNSIGNED, otherwise, uncheck "Unsigned" param in your table structure mysql. And then:
UPDATE table SET id=(id - max_id) * (-100000);
example: UPDATE table SET id=(id - 28542) * (-100000);
And
UPDATE table SET id=(id/100000);
Results, before:
id name age adress date
1 carl 12 something 2015-11-10
2 lisa 51 something2 2015-10-09
3 steven 32 something3 2014-12-29
After:
id name age adress date
28541 carl 12 something 2015-11-10
28540 lisa 51 something2 2015-10-09
28539 steven 32 something3 2014-12-29
So the new record will be
28542 neil 25 something4 2016-01-12
Related
Table1:
ID Name XCount
1 Bob 12
2 Jack 13
Table2:
ID XCount YCount
1 14 1
2 22 15
When i insert a new record (on this example as a third record) to the Table1 then;
if that third record's XCount value is equal to some row's XCount Field on the Table2;
Than that rows YCount field will be updated as ycount=ycount+1 on the Table2.
Input: Table 1
ID Name XCount
3 George 14
Output: Table2:
Id Xcount YCount
1 14 2
2 22 15
I tried a couple of times with "After Insert Macro" but can't handle it. Can anyone help me to overcome this situation?
I'm currently working on a query that looks like this. There are two tables - members and member_gathering.
SELECT id, city_id, name FROM members
WHERE "id" = "member_id" IN
(
SELECT "member_id" from member_gathering
GROUP BY "member_id"
HAVING COUNT(DATEDIFF("visited","joined">=365))>=5
ORDER BY "member_id"
)
ORDER BY id*1;
The goal is to have an output of all IDs satisfying the condition of being in more than 5 groups, in which a member is active for more than a year. Being active means having a difference between "visited" and "joined" columns (both are TIMESTAMP) for more than a year (I set that as 365 days).
However, after running, this code shows all the rows in a members table (though manual check of both tables shows that some rows do not satisfy both conditions at the same time).
Any ideas on how to improve the code above? I'm not sure if I can use 'nested' condition inside COUNT(), but all other variants used before show either NULL values or returned all rows in the table, which is obviously not right. Also, I was thinking that problem might be with DATEDIFF function.
All suggestions are welcome: I'm a newbie to MySQL, so I'm not that familiar with it.
UPD: data sample:
1) members
id city_id name
2 980 Joey
5 980 Carl
10 1009 Louis
130 1092 Andrea
2) member_gathering
member_id gathering_id joined visited
2 1 2010-01-01 00:00:00 2010-02-01 00:00:00
2 2 2010-01-01 00:00:00 2010-02-01 00:00:00
5 2 2010-01-01 00:00:00 2010-02-01 00:00:00
10 3 2010-01-01 00:00:00 2010-02-01 00:00:00
130 1 2010-02-01 00:00:00 2013-02-01 00:00:00
130 2 2010-02-01 00:00:00 2013-02-01 00:00:00
130 3 2010-02-01 00:00:00 2014-02-01 00:00:00
130 4 2010-02-01 00:00:00 2018-02-01 00:00:00
130 5 2010-02-01 00:00:00 2015-02-01 00:00:00
Expected result would be only ID 130, thus: 130, 1092, Andreana.
I believe you first need to find all records where datediff is 365 days or more. Then find members who have 5 or more such instances. This needs both WHERE and HAVING clause:
SELECT id, city_id, name
FROM members
WHERE id IN (
SELECT member_id
FROM member_gathering
WHERE DATEDIFF(visited, joined) >= 365
GROUP BY member_id
HAVING COUNT(*) >= 5
)
You could use this way
SELECT id, city_id, name FROM members
WHERE member_id IN
(
SELECT member_id from member_gathering
GROUP BY member_id
HAVING SUM(DATEDIFF(visited, joined) >= 365)>=5
ORDER BY member_id
)
You should use separated expression for count differente category of datediff and remmeber that count work for not null values so if you want obtain the totale for true values you should sue SUM
I have a DB which is an Attendance List for a Training Program. In this list - First Subscriptions gets Priority - and LOCAL USERS have priority too.
DB:
ID NAME SUBSCRIPTION DATE COUNTRY
1 JOHN 2018-04-05 12:00:00 USA
2 MARY 2018-04-05 12:30:00 CANADA
3 CARL 2018-04-05 13:00:00 USA
I need a way to order the table like this:
ID NAME SUBSCRIPTION DATE COUNTRY
1 JOHN 2018-04-05 12:00:00 USA
3 CARL 2018-04-05 13:00:00 USA
2 MARY 2018-04-05 12:30:00 CANADA
CARL is from USA then I need to give priority to him, even Mary makes his subscription early.
any idea?
** IMPORTANT: Country priority changes according to the location of the training. (here the location is "USA", but can be any country)
I tried this:
SELECT * FROM SUBSCRIPTION_TABLE ORDER BY COUNTRY = 'LOCAL_COUNTRY_VAR', SUBSCRIPTION_DATE
but it did not work.
You appear to want to order first by being in the USA and then by the date:
In MySQL, you can do:
order by (country = 'USA') desc, date
I would recommend creating a separate table with the location and a priority code (integer). Inner join the two and then order by the priority code ascending. USA would have priority 1, CANADA would be 2.
Table 1
Customer id city
John 1 LA
Nancy 2 NULL
Table 2
Customer $ in the pocket
John 20
Nancy 30
I am wondering what happen if Table 1 natural join with Table 2? My guess is that the result would be 4 attributes and both John and Nancy will appear.
But my friend told me that only John will appear, Nancy won't because there is a null value.
In the case above, your friend is wrong, you are right!
Let's see a case where it would be otherwise:
Table 'Customer'
Id Name AccNo
1 John 44
2 Nancy NULL
Table 'Account'
AccNo $_in_Pocket
44 20
45 30
Here, with a natural join, we would get all attributes for John but Nancy would be missing from the results.
I have table like below :
Application_Number Id_Number1 Name
1 123 John
2 456 Alan
3 789 Charlie
4 111 Patrick
5 222 Robert
Then i would like to update record in one of rows become like this :
Application_Number Id_Number1 Name
1 123 Alias 1
2 456 Alias 2
3 789 Alias 3
4 111 Alias 4
5 222 Alias 5
if i have more than one million record do i need update syntax or any another way? I'm using SQL2008
Thanks
Select Application_Number
,Id_Number1
,'Alias' + CAST( ROW_NUMBER()
OVER (ORDER BY Application_Number) AS Varchar) AS Name
FROM Table Name