Ask for Int value in any of my columns from my Table - sql

Lets say i have a table named 'Test' with these columns and values
ID Apple Pear Pineapple Orange
1 2 3 4 4
2 1 12 1 0
Now I want to make a SELECT statement to return all the rows where there is a value of 4 in any column.
I could do it like this, but I have many columns to check, so I need to find a other way to do this.
SELECT * FROM Test
WHERE test.apple = 4 or test.apple = 4 or test.pineapple = 4 or test.pineapple = 4

Try this:
SELECT *
FROM Test
WHERE 4 IN (Apple, Pear, Pineapple, Orange)

One method is to use in:
select t.*
from test t
where 4 in (t.apple, t.pear, t.pineapple, t.orange);
You still need to list the columns, but they just go in the in list.

Related

Select * in Postgres not selecting duplicates; how to get duplicates?

when I run this query:
SELECT item_id
FROM cart
WHERE user_id = 6
I get: 2 3 4 4 4 4 4 3 3 2 2
---when I run:
SELECT * FROM items
WHERE id IN(
SELECT item_id FROM cart
WHERE user_id = 6
I get:
1 Apple 0.40 red fruit
2 Banana 0.30 yellow
3 Mango 1 yellow fruit
4 Carrot 0.50 orange vegetable
I'd like to get it where it sends the duplicates too for how many times it is there. I had the same problem with SUM where it only add the values once but not for any duplicates. How is there a way to get the duplicates to be returned also? and hopefully with SUM too
Sounds like you want a join:
SELECT itm.*
FROM items itm
JOIN cart crt on crt.item_id = itm.id
WHERE crt.user_id = 6;

Select rows where column is two different values

I have a table like this:
id
item
value
1
Apple
1
2
Apple
7
3
Banana
5
4
Banana
6
5
Pear
1
6
Pineapple
7
How do I only select rows that have both [1,7] as values?
Output:
id
item
value
1
Apple
1
2
Apple
7
Edit: You can try using a sub query like this
SELECT * FROM TABLE
WHERE ITEM IN
(SELECT ITEM FROM TABLE WHERE VALUE IN (1,2)
GROUP BY ITEM HAVING COUNT(ITEM) = 2)

Specific Row at the top line

There is a table in SQl database which shows the result the way below:
ID Name value
1 Chips 100
2 Chocolate 50
3 Ice Cream 200
4 Burger 40
but I want to display it like in this way:
Name value
Chocolate 50
Chips 100
Ice Cream 200
Burger 40
how can I do this in sql server?
You can use ID to do the sorting like below
ORDER BY CASE ID
WHEN 2 THEN 1
WHEN 1 THEN 2
WHEN 3 THEN 3
WHEN 4 THEN 4
ELSE 5
END
If there is dynamic rows (lot of rows), you can use same like below
ORDER BY CASE ID
WHEN 2 THEN 1
ELSE 2
END
The else part will take care of the rest of the rows.
You can use CASE in ORDER BY to make sure the record with name = 'Chocolate' comes first and the rest is sorted by ID.
select name, value
from table1
order by case name
when 'Chocolate' then -1
else ID
end
Try this
SELECT * FROM `test` WHERE 1 ORDER BY name = 'Chocolate' DESC

Replace for-loop in SQL

I have a table in a database. For example table of user IDs and right IDs:
UserId RightId
---------------
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
4 1
4 2
5 1
6 1
6 2
What is the best way to insert for each userId new rightId 4?
I heard that using while or for loops is not the best way to do such thing.
Can you please show me an example how to solve such problem with JOINs and SETs for example?
How about something like
INSERT INTO MyTable (UserID, RightID)
SELECT DISTINCT
UserID,
4
FROM MyTable
SQL Fiddle DEMO
If you simply want to change every entry in the RightId column you could try something like this:
UPDATE <table_name> SET RightId=4;

Get rows with single values using SQlite

By using SQlite, I'd like to get all rows that show in a specific column only one single distinct value. Like from following table:
A B
1 2
2 1
3 2
4 3
5 1
6 1
7 2
8 4
9 2
Here I'd like to get only row Nr. 4 an 8 as there values (3 and 4) occur only once in the entire column.
You could use a query like this:
SELECT *
FROM mytable
WHERE B IN (SELECT B FROM mytable GROUP BY B HAVING COUNT(DISTINCT A)=1)
Please see fiddle here.
Subquery will return all B values that are present only once (you could also use HAVING COUNT(*)=1 in this case), the outer query will return all rows where B is returned by the subquery.