I have myTable like the belowcode randomNumber
(1) 86
(2) 13
(3) 27
(4) 35
(5) 42
(6) 58
(7) 61
(8) 78
(9) 95
(10) 98
(11) 53
[/code]Now I have a new randomNumber “49” which is coming from “rand(11,99);” in php.
I like to insert a new record “(12) 49” into myTable.
Here I have a problem, i.e, the randomNumber “49” should not be doubled in the column.
SELECT count(*) FROM myTable WHERE randomNumber=49
Although I can check it is doubled or not with the SQL above, I have to check againg with a new randomNumber which is not “49”
I like to put a new randomNumber which is not doubled.
How can I do it with your help?
You’ll have to check if exists whether you like or not. Unless id is autonumeric and what needs to stay unique is the combination of id and randomNumber. But for your description, it is unlikely that this is the case
[quote=“r937, post:7, topic:242218”]
do you know how to research it?
[/quote]I did googling with the keyword “mysql status code”.
As I read some of search result, I think I found this is not the correct keyword for googling.
INSERT INTO myTable (randonNumber)
VALUES (49)
WHERE NOT EXISTS (
SELECT randomNumber
FROM myTable
WHERE randomNumber = 49
);
In PHP most extensions for interacting with a database will have a function that will tell you how many rows were inserted. If the extension doesn’t have such a function then before the INSERT you’ll need to do a SELECT query to count the number of rows in the table. After the INSERT you’ll have to do another SELECT to get the current number of rows in the table. If there is now one more then the new row was inserted. If there isn’t any change in the row count then that number already existed.
How many records will get inserted into the table? When the records are being used, in what way are they being used? Depending on how they’re being used you might be better off having PHP randomize the order in which the records are displayed