I have a table with a limit of 3 rows per user, so each user can only add 3 elements in the table.
I wrote something like:
INSERT INTO 'MyTable' ('ID', 'eMail', 'ISBN')
VALUES ('333', 'a#d', '222') , ('433','e#r', '223')
but the problem is that I have no idea how to set the limit for the insertion. I just know that I cannot use LIMIT because it only works with a SELECT query.
I don't know is there any single query that will handle limit and insert at once or not? I would rather count rows by each user id before inserting new rows for that user.
For Example:
SELECT ID, COUNT(*) as CNT from `MyTable` GROUP BY ID HAVING COUNT(*) < 3 ORDER BY `ID` DESC
So the above query will return only the ID->respective row count of that ID, with this information you can easily filter out which user can have new row on your table and which should be discarded?
ID CNT
333 2
899 1
443 2
INSERT INTO 'MyTable' ('ID', 'eMail', 'ISBN') VALUES ('333', 'a#d', '222') , ('899','e#r', '223'), ('443','f#r', '224')
Related
I have two tables x and y, x have the ID and many other columns, however y only have the ID similar as x table and then this ID is mapped to Many values
My Insert statement looks like this
INSERT INTO `table`
(`id`,
`other_name`)
VALUES
(select id from another_table where name = 'something'`,
('WALLETAB',
'SBTRADER',
'SBTRDACKING'));
expected result
1 | WALLETAB
1 | SBTRADER
1 | SBTRDACKING
I take ID from another table which already have data and this another table some different data associated with this table
You could fetch id from another table to be used in insert statement by using limit 1, something like:
select id from another_table where name = 'something' limit 1
However, to insert all 3 rows you will need a multiple insert in a single statement.
insert into `table` values
((select id from another_table where name = 'something' limit 1), 'WALLETAB'),
((select id from another_table where name = 'something' limit 1), 'SBTRADER'),
((select id from another_table where name = 'something' limit 1), 'SBTRDACKING');
See fiddle: https://www.db-fiddle.com/f/gYvrxdsDxVQPkZM2o8YRT1/1
It feels a lot of duplication. You can simplify it by either using variable or CTE. The following query utilizes CTE which only usable on mysql 8+:
insert into `table` (id, other_name)
with
other_id as (
select id from another_table where name = 'something'),
merged as (
select id, other_name from other_id join
(select 'WALLETAB' as other_name
union select 'SBTRADER'
union select 'SBTRDACKING')
as other_temp)
select * from merged;
The CTE above fetch the id on other_id. The union-select pairs is then used to create 3 rows containing 'WALLETAB', 'SBTRADER', and 'SBTRDACKING' respectively. Then both of them joined to get 3 rows with varying value on other_name but has id as 1.
See fiddle: https://www.db-fiddle.com/f/xgQta17bGphHAB81N2FNwX/1
I have the following MySQL statement that returns a list of user_id's
SELECT user_id FROM user_links WHERE linked_user_id='1234'
The user_id never contains the linked_user_id that I use in the where clause. So I want to always add the from clause "linked_user_id" in the result but also want to sort by the user_id ASC.
Is there a way to do this in the MySQL statement itself or do I have to do this with some other scripting?
You can modify your condition to add that user_id:
SELECT user_id FROM user_links WHERE '1234' in (user_id, linked_user_id)
Let's sort it accordingly:
SELECT user_id
FROM user_links
WHERE '1234' in (user_id, linked_user_id)
ORDER BY user_id
Or you can even guarantee that the linked user id will be the last row by:
SELECT user_id
FROM user_links
WHERE '1234' in (user_id, linked_user_id)
ORDER BY user_id = '1234', user_id
Use UNION
SELECT user_id FROM user_links WHERE linked_user_id='1234'
UNION ALL
SELECT '1234'
I have 3 Mysql tables.
A table with the classes and the labs and their id.
A table with the teachers_list and their subject.
A table which is going to be the schedule.**
I want to randomly assign one of the physicists to one of the physics labs on my third table which is going to be the schedule.
INSERT INTO schedule(teacher_name, class_id)
VALUES (select teacher_name from teachers_list where subject="Physicist” order by rand() limit 1,
select id from lab_list where lab="Physics_lab" order by rand() limit 1);
**This one doesn't work :(
Can you help me?**
I think that you want the insert ... select syntax, along with a subquery:
insert into schedule(teacher_name, class_id)
select
(
select teacher_name
from teachers_list
where subject = 'Physicist'
order by rand()
limit 1
),
id
from lab_list
where lab = 'Physics_lab'
I've got query.
SELECT * FROM '.PRFX.'sell
WHERE draft = "0" '.$e_sql.'
AND ID NOT IN (SELECT id_ FROM '.PRFX.'skipped WHERE uid = "'.$u.'")
AND ID NOT IN (SELECT id_ FROM '.PRFX.'followed WHERE uid = "'.$u.'")
ORDER BY raised DESC '.$sql_limit;
I want to add 3 records by the lowest number of refreshes; best on 5th position
they must be unique (so if you connect two UNION ALL...)
Firstly, you need make your SQL more readable. Something like this
SELECT * FROM sell
WHERE draft = 0
AND ID NOT IN (SELECT id_ FROM skipped WHERE uid = '0')
AND ID NOT IN (SELECT id_ FROM followed WHERE uid = '0')
ORDER BY raised DESC LIMIT 15
Then, what do you want? Add data to sell table through single request? This can be done with such request
INSERT INTO sell (key1, key2, keyN)
VALUES
('aaa', 'bbb', 'ccc'),
('ddd', 'eee', 'fff');
-- and so forth.
I have a database with one table called "user" having two fields:
"id" (type: INTEGER, PRIMARY KEY)
"name" (type: VARCHAR(32))
I want to Write a standard SQL query which retrieves the second highest value of "id" from the "user" table. The value returned should be represented using the column name "id".
I have tried this but it gives me all ids:
SELECT `user`.`id`
FROM `user`
ORDER BY `user`.`id` ASC
LIMIT 0 , 30
some example data in my table:
id name
----------------
1 john
2 david
3 mike
I want to get '2' but now i'm getting :
id
----
1
2
3
I can do it with help of PHP but I want to know the way with mysql (SQL).
thanks
SELECT id
FROM `user`
ORDER BY id DESC -- start with highest
LIMIT 1 -- show only 1 row
OFFSET 1 ; -- but skip the first (skip 1 row)
SELECT id FROM user ORDER BY id ASC LIMIT 1, 1
select max(id) from user where id < (select max(id) from user);
select max(id) from user where id not in (Select max(id) from user);
SELECT id
FROM `user`
ORDER BY id ASC
LIMIT 1, 1
Using limit you can set an offset and the number of records you like being returned.
How 'bout
Select max(id) from user
where id <> (select max(id) from user)
This may work:
select max(id)-1 from user;