I'm probably having a bad day, but this is somehow escaping me:
I want to return the second row in this table only.
userId val1 val2
1 11 12
2 13 14
3 13 15
4 16 17
Using SELECT * FROM table WHERE val1=13 AND val2=14 obviously returns 2 rows, the second and third. Whats the correct way to select ONLY the second row? Where val1 is 13 and val2 is 14?
EDIT: I'm an idiot.
Just use SELECT * FROM table WHERE val1=13 AND val2=14like you already mentioned in your question, because in fact, it actually returns only row number 2.
If it has been a very bad day & there is a typo in your question & val2 in third row also equals 14 - the only way your query would return two rows, then this would do what you want
SELECT *
FROM table
WHERE val1=13 AND val2=14
ORDER BY userId
LIMIT 1;
If you get 2 rows, then there must be 2 rows match the condition.
Maybe you could try:
select count(*)
from table
where val1=13 AND val2=14;
to show the size of result set.
Related
I've been trying to figure out a way to select lines that are unique based on the values of two columns. For example, here is a sample/example of my file:
s r
10 12
10 13
14 10
10 12
14 10
12 10
And this is what I want my output to look like:
s r
10 12
10 13
14 10
I've tried to use this code
SELECT * FROM `message` WHERE (`s_id`=$b_id or 'r_id'=$b_id) GROUP BY
r_id
However, it only select 10,12 10,11 and i also needed 14,10 i mean the third row
in this case $b_id is obtained from session and it may be s or r
You can try to use DISTINCT
SELECT DISTINCT s_id,r_id FROMmessageWHERE (s_id=$b_id or 'r_id'=$b_id);
always try to specify which column needed instead of * (all) it improve the performance
Do it like this
select distinctrow s_id,r_id from table where s_id = 10 or r_id = 10
or else use "having" clause and group by both columns
After two days struggling i am succeeded in finding out solution to my problem
SELECT * FROM `message` WHERE s=$b_id or r=$b_id AND (r and
s)<>(s_ and r) GROUP BY s,r
i have this table called "tblpercentage"
PercentID Dividend Rebates Interest
1 40 10 2
2 60 20 5
this is my query
SELECT MAX(PercentID)As PercentID,Interest FROM tblpercentage
but the problem is. it can only select PercentID = 2 and interest = 2 instead if 5. but i want is select all row with the max id. can anyone help me.
for get the row related to the max id you should use a subquery
select PercentID,Interest from tblpercentage
where PercentID = (SELECT MAX(PercentID) from tblpercentage)
Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.
I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)
I want to achieve one thing though I'm not sure if it's possible.
So let's say I have a table with few columns, but only two of them are of interest to me right now. Example of table:
Column 1 | Column 2
blabla | blablahhhhh
wor154 | blablahhhhh
word123 | word12435564
something | some4565
What I want to achieve, is to select all fields where first 5 or more symbols of value of Column 2 don't match with first 5 or more symbols of value of Column 1. So I don't want to select rows where 5 or more symbols of value of Column 1 match 5 or more symbols of value of Column 2. In example, query should return only 2nd and 4th rows
So, is it possible and if it's, how it can be achieved. Thank you.
I'd go with a SUBSTRING():
SELECT col1 FROM table WHERE SUBSTRING(col1, 1, 5) <> SUBSTRING(col2, 1, 5);
You can use something similar to this:
select *
from table1
where substring(column1, 1, 5) != substring(column2, 1, 5)
See SQL Fiddle with Demo