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'
Related
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')
I have a table that contains transaction data. The rows with the same 'group_id' are a part of the same transaction. I am running the following SQL query to show all the transactions:
SELECT * FROM transactions
When I run this query I get as expected a list of all the transactions. But this large list makes it difficult to seperate the data with a different group_id from the other data.
For that reason I want to add an empty row at the end of the group_id, so I get:
1
1
(empty row)
2
2
2
instead of:
1
1
2
2
2
Can someone help me with this?
Here is my database:
http://sqlfiddle.com/#!9/b9bf79/1
I do not suggest you do this at all but if you just want to separate two groups you could do this:
SELECT * FROM transactions WHERE group_id = 1
UNION ALL
(SELECT '','','','','','')
UNION ALL
SELECT * FROM transactions WHERE group_id = 2
Obviously this can added to if there are more group ids in the future but it is not a general purpose solution you are really better off dealing with appearance issues like this in application code.
you can use (abuse) rollup.
SELECT *
FROM transactions
group by group_id, id
with rollup
having group_id is not null
this will insert a row with id set to null after each group_id.
mysql will also sort by group_id because of the group by.
The group by id` makes sure that all rows are shown (your schema does not show it, but I assume id is unique? Otherwise you need to add other fields)
However only id will be null in the extra rows. The other columns repeat the value above.
You can filter them like this:
SELECT
id,
case id is not null when true then date else null end as date,
case id is not null when true then group_id else null end as group_id
-- ....
FROM transactions
group by group_id, id
with rollup
having group_id is not null
Alternatively:
select * from
(SELECT *
FROM transactions
union all
select distinct null, null, group_id, null, null,null from transactions
) as t
order by 3,1
but null values are sorted first, so the "gap" is in front of each section
How to select all entries but the last one in MySQL?
I tried the following statement:
SELECT *
FROM table name
ORDER BY id
SELECT id FROM lot_master WHERE `id auto increment` ORDER BY id decs LIMIT 1
How can I achieve this?
select Name from shopping_cart where id<>(select max(id) from shopping_cart);
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;
I have 9 fields and I need to see all the data from these fields which have a particular set of IDs. Could any one tell me the SQL query for it?
Ex: Database contains 100 records. I need to select a list of 20 IDs from the field BusID and it's corresponding rows.
SELECT *
FROM `Buses`
WHERE `BusID` I am stuck after this.. how do I put in the list of 20 BusIds here?
If you know the list of ids try this query:
SELECT * FROM `Buses` WHERE BusId IN (`list of busIds`)
or if you pull them from another table list of busIds could be another subquery:
SELECT * FROM `Buses` WHERE BusId IN (SELECT SomeId from OtherTable WHERE something = somethingElse)
If you need to compare to another table you need a join:
SELECT * FROM `Buses` JOIN OtheTable on Buses.BusesId = OtehrTable.BusesId
You can try this
SELECT * FROM Buses WHERE BusID in (1,2,3,4,...)
I strongly recommend using lowercase field|column names, it will make your life easier.
Let's assume you have a table called users with the following definition and records:
id|firstname|lastname|username |password
1 |joe |doe |joe.doe#email.com |1234
2 |jane |doe |jane.doe#email.com |12345
3 |johnny |doe |johnny.doe#email.com|123456
let's say you want to get all records from table users, then you do:
SELECT * FROM users;
Now let's assume you want to select all records from table users, but you're interested only in the fields id, firstname and lastname, thus ignoring username and password:
SELECT id, firstname, lastname FROM users;
Now we get at the point where you want to retrieve records based on condition(s), what you need to do is to add the WHERE clause, let's say we want to select from users only those that have username = joe.doe#email.com and password = 1234, what you do is:
SELECT * FROM users
WHERE ( ( username = 'joe.doe#email.com' ) AND ( password = '1234' ) );
But what if you need only the id of a record with username = joe.doe#email.com and password = 1234? then you do:
SELECT id FROM users
WHERE ( ( username = 'joe.doe#email.com' ) AND ( password = '1234' ) );
Now to get to your question, as others before me answered you can use the IN clause:
SELECT * FROM users
WHERE ( id IN (1,2,..,n) );
or, if you wish to limit to a list of records between id 20 and id 40, then you can easily write:
SELECT * FROM users
WHERE ( ( id >= 20 ) AND ( id <= 40 ) );
I hope this gives a better understanding.
You're looking for the IN() clause:
SELECT * FROM `Buses` WHERE `BusID` IN (1,2,3,5,7,9,11,44,88,etc...);
Try the following code:
SELECT *
FROM users
WHERE firstname IN ('joe','jane');
You want to add the IN() clause to your WHERE
SELECT *
FROM `Buses`
WHERE `BusID` IN (Id1, ID2, ID3,.... put your ids here)
If you have a list of Ids stored in a table you can also do this:
SELECT *
FROM `Buses`
WHERE `BusID` IN (SELECT Id FROM table)
I have 3 fields to fetch from Oracle Database,Which is for Forex and Currency Application.
SELECT BUY.RATE FROM FRBU.CURRENCY WHERE CURRENCY.MARKET =10 AND CURRENCY.CODE IN (‘USD’, ’AUD’, ‘SGD’)