How to say or in MySQL statement - mysql

I'm trying to match two choices. "One or the other"
SELECT * FROM course WHERE category='blue' || category='red' ORDER BY id DESC LIMIT 5
That is my first thought is to use
||

SQL does support logical operators as well.
By the way, you can shorten this statement to
SELECT * FROM course WHERE category IN ('blue','red') ORDER BY id DESC LIMIT 5

use the word or instead of ||.

Related

In mysql how do I only search through the first 10 records

I am writing a query, but I only want to search the first 10 records in the table. I know that in a select limit usually limits the records, but it doesn't work for this instance.
eg
SELECT * FROM `logon` WHERE `username`='superman' ORDER BY `user_id` LIMIT 10
The above line will never work because the query only returns one.
I only want to search through the first 10 records, and limit doesn't work in this case.
So how do I limit my search to the first 10 records?
SELECT * FROM
(SELECT * FROM `logon` ORDER BY `user_id` LIMIT 10) as temp
WHERE temp.`username`='superman';
You can simply write a subquery which returns the "first" 10 records and put a where clause on the result set.
SELECT *
FROM `logon`
WHERE `username`='superman'
and user_id in (select user_id from logon order by user_id limit 10)
(I haven't tried this, but I think it's the fastest way to do this)
Use order by
SELECT * FROM `logon` WHERE `username` = 'superman' ORDER BY user_id LIMIT 10
Not the right Way...but can be done...
SELECT *
FROM logon
WHERE username='superman' AND srno BETWEEN 1 AND 10;

How to select words of the same CHAR_LENGTH in MySQL-PHP?

In order to use in a multiple choice test on Chinese Language, I need to select 5 words, all of them must have the same number of characters, one for the question, four for options. What I have been doing is:
SELECT hanzi, CHAR_LENGTH(hanzi) chlen FROM wordlist ORDER BY RAND() LIMIT 1
From here I get chlen to use it in another statement:
SELECT hanzi FROM wordlist WHERE CHAR_LENGTH(hanzi)=$chlen ORDER BY RAND() LIMIT 4
Can I do this using only one statement?
You can use subquery to achieve this:
SELECT hanzi FROM wordlist WHERE CHAR_LENGTH(hanzi) in (SELECT CHAR_LENGTH(hanzi) chlen FROM wordlist ORDER BY RAND() LIMIT 1) ORDER BY RAND() LIMIT 4
I haven't tested it, so it might give some errors; but at least you can get a idea from this.
Try
SELECT * FROM table ORDER BY length(column);
Ref
Documentation on the length() function, as well as all the other string functions, is available here.

SQL First() Function

I am using phpMyAdmin to write some SQL code that I thought was simple but proving to be a headache. I'm using this tutorial to help me out. My goal is to get the first and last columns id's from a result set. When I do this query I get 5 rows starting at 15 and going through 11.
SELECT id
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
However, when I try this query I get an error #1064: "You have an error in your SQL syntax."
SELECT FIRST(id)
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
Something like this maybe?
SELECT min(id), max(id)
from (
select id
from boardPost
where recipientId = 1
order by id desc
limit 0,5
) t
I think that is what you want?
Select id from boardPost order by id asc limit 1
and
Select id from boardPost order by id desc limit 1
If you just want the first and the last id of a result set, you could consider this:
SELECT MIN(id) firstId, MAX(id) lastId FROM someTable WHERE aField = 1;
Note that it'll only work if you do use and ORDER BY an AUTO_INCREMENT field, else you might get unexpected results.
It'll only work with the full set. If you need the first and last id of a limited one, you're probably better of using 2 queries with ASC and DESC order and LIMIT 1.
MySQL does not support the FIRST() function. You will need to use the workaround they specified in that tutorial (using ORDER BY and LIMIT)
In some situations (like mine, that first brought me here), there are some rarely-used MySQL "windowing functions" such as FIRST_VALUE and LAST_VALUE that may provide the functionality you're seeking.
(Here's more info on Window Function Concepts and Syntax, and the Wikipedia description of the term.)

Return only one tuple of SQL query result

I want to do a query like
select * from chr2;
but only have MySQL return the first tuple (or an arbitrary) tuple instead of all of them.
How do I do it?
Use the LIMIT clause:
SELECT * FROM chr2 LIMIT 1;
If you want an arbitrary row returned, you have to sort your rows by an random col like this (MySQL docu):
SELECT * FROM chr2
ORDER BY RAND()
LIMIT 1;
On large tables, however, you might run into performance problems with this, as there a random value has to be created for each row and the table has to be sorted according to this column.
Try this ::
select * from chr2 limit 1

Mysql query based on a list of specific random numbers?

I'm trying to do a query based on specific numbers which at this stage I only need to hardcode into the page as comma separated values.
2312,2431,2439,2440,2442,....
But I can not use between because the numbers in between may not be relevant
So how do I do a query of this type?
$sql= "SELECT * FROM table
WHERE pc=2431 OR pc=2439 OR
pc=2440 OR pc=2442 OR
pc=2443 OR pc=2444 OR
pc=2445 OR pc=2446 OR
pc=2312 AND v=1
ORDER BY id DESC
LIMIT $offset, $rowsperpage";
I tried this and kind of works but there must be a better way.
Thanks in advance
Indeed there is... You can use the IN operator so that the query becomes:
SELECT *
FROM table
WHERE pc IN (2431,2439,2440,2442,2443,2444,2445,2446,2312)
AND v=1
ORDER BY id DESC
LIMIT $offset, $rowsperpage
Simples :o)
For more info check out the MySQL documentation at http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_in
I would suggest a little improvement to other answers:
$pcs = array(2431,2439,2440,2442,2443,2444,2445,2446,2312);
"SELECT *
FROM table
WHERE pc IN (" . implode(",", $pcs) . ")
AND v=1
ORDER BY id DESC
LIMIT $offset"
this way if later your ids will change and you have several queries using them you will have to update only one place instead of N (where N is number of queries). If you use these ids in many different files, consider making a constant