updating id to max(id)+1 [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
can someone please help me with the below mysql query:
I want to build a query like:
UPDATE users
SET ref_id = (select max(ref_id)where `role_id`=5) +1
WHERE user_id =102
AND `role_id`=5

where your FROM clause in subquery???
UPDATE users
SET ref_id =
(select max(ref_id) FROM ???? where role_id=5) + 1
WHERE user_id =102 and role_id=5

Ok I tried a similar request and I had this MySQL error :
1093 - You can't specify target table 'my_table' for update in FROM clause
Try using the following request :
UPDATE `users`
SET
`ref_id` =
(
( SELECT max_id FROM ( SELECT MAX(ref_id) AS max_id FROM users WHERE `role_id`= 5) AS sub_selected_value )
+ 1
)
WHERE `user_id` = 102 AND `role_id` =5
Please keep in mind that may not be the most efficient solution if performance matters because it uses temporary tables.

Related

php code to delete value table 2 from value of table 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am php beginner.
I would like to read email contain in table 1 and with the value selected deleted the corresponding email of the table 2 ?
May you provide me some info to manage it ?
select email_table1 from table1.
delete from table2 where email = email_table1.
Thanks
Greg
There's little information to form an answer but you could use a sub select.
Something like this presuming you're using MySQL or similar.
DELETE FROM `table1` WHERE id = (
SELECT `referenceID` FROM `table2` WHERE `email` = 'the#email.com' LIMIT 1
) LIMIT 1;

Display/create a blank row in between data set and breack the table data using php/mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am using one table and two queries to display the below output.
Here I have used 1st query to display the morning/middle/evening and other to get the user details.
I want to user a empty/blanks row after the last morning/middle which need to have this output
Expected output:
Can anyone help me on this.
Not knowing your SQL statemanets, i would only advise to use
UNION ALL
(See Docu here)
So your Statement would look somehow like that:
Select Col1, Col2
from tbl1
UNION ALL
Select NULL as Col1, NULL as Col2
UNION ALL
Select Col1, Col2
from tbl2

Select and sort data from one table on certain columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking to see if it is possible to select data from table A with a sort so the order of the data changes.
I then want to make a loop so it will go through each row and add it into table B.
Is this possible.
Use a variable #row_number to store the incremented num for your expected sorting.
SET #row_number = 0;
SELECT (#row_number:=#row_number + 1) AS num, Column1
FROM TableA
ORDER BY Column1
This result can be store in temporary table and based on that you can do loop/insert into the TableB

Show a random mysql row, but not the same twice [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a database that holds all my information with questions, users, answers etc.
I've been thinking all day how to solve this problem of mine. Okey, so the problem is like this:
Let's say I have a table in my DB, with questions (300++) and the user that logs on will get a random question shown. But never the same question again, so I'll need to store this information in a separate table with the user-ID and the question-ID. And I'll need to create another table that stores the answers to the questions.
So how would this PHP/MYSQL-code look like? Because I'll need to find a random question that hasnt been shown to the same user again.
If something is unclear, please let me know. And thanks in advance
You can use RAND() function and NOT IN
Example:
SELECT * FROM `questions` WHERE id NOT IN (5, 3) ORDER BY RAND() LIMIT 1;
You have to fetch question id using sub query
Example
SELECT * FROM `questions` WHERE id NOT IN (SELECT question_id FROM shown_questions WHERE user_id=1) ORDER BY RAND() LIMIT 1;
You can use an outer join to select items that have not been used already. It might be a bit faster than NOT IN if you set up the indexes:
SELECT questions.* FROM questions
LEFT JOIN shown_questions
ON (questions.id=show_questions.question AND user_id=42)
WHERE shown_questions.question IS NULL
ORDER BY RAND() LIMIT 1;

How could I filter duplicates? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
INFORMATION:
TABLE = giveaway
FORM 1 = id
FORM 2 = username
FORM 3 = userid
FORM 4 = wish
END OF INFORMATION
I am trying to make it filter through duplicate usernames and userids
NOTE: people enter the userid and the username (it is for a giveaway)
can anyone post me some code to do that?
Try this one:
SELECT `username`,`userid` FROM `giveaway` group by `username` HAVING COUNT(`username`) > 1 UNION SELECT `username`,`userid` FROM `giveaway` group by `userid` HAVING COUNT(`userid`) > 1
drop table test;
/
create table test
(
id number,
username varchar2(20),
userid number,
wish varchar2(40)
);
/
insert into test
values(1,'A',1,'ZXX');
/
insert into test
values(1,'A',1,'YYY');
/
insert into test
values(1,'B',2,'ZXX');
/
SELECT * FROM
(
SELECT ID ,USERNAME,USERID,COUNT(*) OVER (PARTITION BY USERNAME,USERID) AS CNT
FROM TEST
)
WHERE CNT <2
This will give you unique values

Categories