This question already has answers here:
php/mysql multiple order by
(3 answers)
How to define a custom ORDER BY order in mySQL
(4 answers)
Closed 6 years ago.
I'm trying to display items from my database, here's an example
user | 123 | abc
Jack | 0 | b
John | 0 | c
Doe | 1 | a
and how I want it displayed on site is something like this
user | 123 | abc
Doe | 1 | a
Jack | 0 | b
John | 0 | c
I want it orderying by abc(abc=1) AND 123 (cat then dog then chicken)
Tips:
abc being either 0 or 1
123 being multiple things like: chicken, dog, cat etc..
The field isn't named 123 it's just an example and this is the query i'm using right now: SELECT * FROM users order by paid=1, animal='cat', animal='dog' It's bringing paid users up first but isn't ordering by animal
drop table users;
create table users (name varchar(5),animal varchar(10),paid int);
insert into users values
('abc','dog',1),
('def','dog',0),
('ghi','chicken',0),
('jkl','cat',0);
select * from users
order by paid desc,
case
when animal = 'cat' then 1
when animal = 'dog' then 2
when animal = 'chicken' then 3
end
result
+------+---------+------+
| name | animal | paid |
+------+---------+------+
| abc | dog | 1 |
| jkl | cat | 0 |
| def | dog | 0 |
| ghi | chicken | 0 |
+------+---------+------+
This is explained in the documentation:
SELECT * FROM t1
ORDER BY key_part1,key_part2,... ;
For more complex ordering, see here:
http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
This should work :
ORDER BY abc, 123
You don't need to write ASC in your case since ASC is the default value.
You need to use ~ORDER BY 123 DESC and abc ASC`
replace test with your table name.
select t.user,t.123,t.abc from test t order by t.123 DESC,t.abc ASC
Demo SQL Fiddle
Output
user 123 abc
Doe 1 a
Jack 0 b
John 0 c
select a.user,a.123,a.abc from test a order by a.123 DESC,a.abc ASC
Related
i have
SELECT s.*
FROM shop
WHERE s.family IN (SELECT s2.family FROM shop s2 WHERE (s2.money like "%k%"))
it does return "ticket" which contanis the k letter but in addition it returns all the other results that doesn't have "k" in it
here is the table of shop :
+----+--------+-------+
| id | family | money |
+----+--------+-------+
| 1 | 1 | card |
| 2 | 1 |Cheque |
| 3 | 2 |coins |
| 4 | 2 |ticket |
+----+--------+-------+
i am using
IN (SELECT s2.family FROM shop s2 WHERE (s2.money like "%k%"));
because i want to show the results as a group of rows with the same family, i got this query from an other question
Did you try:
SELECT *
FROM shop
WHERE money LIKE "%text%";
You are using a statement from a previous question you posted that might not be valid in this situation.
I have 2 tables in my databases, tb_device and tb_label which contain this for example:
tb_device
id (AI) | type | label | etc
-------------------------------------
1 | A123 | 1 | test
2 | A561 | 3 | test2
3 | A777 | 2 | test3
4 | A222 | 3 | test4
tb_label
id (AI) | label
-------------------
1 | Samsung
2 | Apple
3 | Dell
And I already create CRUD form (PHP) which display tb_devices. And this CRUD has a searching form on each columns. Other columns are working for search, but label columns isn't working because it's contain id from tb_label. I want to search label with label name like samsung, apple, dell, not with number. My code for searching:
$sql = "SELECT *
FROM tb_device a, tb_label b
WHERE
type LIKE '%".#$_POST['type']."%' AND
(a.label=b.id AND b.label LIKE '%".#$_POST['label']."%') AND
etc LIKE '%".#$_POST['etc']."%'
ORDER BY type
";
I try to input dell, but the result:
3 | A777 | 2 | test3
Explanation : dell have id number 3, and the result showing id number 3 from tb_device. Is there any solution to show the correct result?
Sorry for bad english
You missed b.label in your query
$sql = "SELECT a.*, b.label
FROM tb_device a, tb_label b
WHERE
a.type LIKE '%".#$_POST['type']."%' AND
(a.label=b.id AND b.label LIKE '%".#$_POST['label']."%') AND
etc LIKE '%".#$_POST['etc']."%'
ORDER BY a.type
";
I believe you should only have the WHERE for tb_label.label = (your user input variable: Dell)
I believe you want to do a
INNER JOIN tb_label
ON tb_device.label = tb_label.id
Now I have 2 tables
STUDENT:
STUDENT_ID | STUDENT_NAME | COURSE_ID
1000 | Anson | 1
1001 | Jnson | 1
1002 | Andy | 2
1003 | Alex | 3
COURSE:
COUSE_ID | COURSE_NAME
1 | P5A
2 | P5B
3 | P5C
Now I would like to produce the result to show the students name in each class
Idea result:
P5A P5B P5C
Anson Andy Alex
Jason
what should I do, I am using php + mysql to build web system
select * from course c left join student s on c.course_id = s.course_id
order by c.course_name, c.course_id
Enumerate the result set. Each time course_id changes, start a new section.
i have a mysql table i.e
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
my question is how can i find the highest subject score, the second last and so on
Note that all the subject fields have int(11) values
Break your database into 3 tables like:
Students:
st_id | name | email
1 | john |#a.com
Courses:
cr_id | name
1 | maths
2 | chemistry
3 | bio
4 | social_studies
StudentCourses:
st_id | cr_id | score
1 | 1 | 20
1 | 2 | 23
1 | 3 | 10
1 | 4 | 15
Now you can do:
SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;
SELECT * FROM <table>
ORDER BY <field> DESC
LIMIT <needed number of rows>
Example:
SELECT * FROM <table>
ORDER BY maths+chemistry+bio+social_study DESC
LIMIT 3
Strictly PHP method: I assume you want to maintain association with field names. In that case, just use asort($row); on each row in your query result, assuming you fetched the row as an array. asort will sort the array from lowest value to highest (with additional flags to tweak the results if needed), while maintaining keys. A foreach loop will then allow you to work with each key/value pair in the sorted order.
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
The query can be for top marks
SELECT id,GREATEST(mark,mark1,mark2) AS `top` FROM `students`
I have two tables one that contains a huge list of items and another that trading for those items.
Here are examples tables:
The main table
| ID | TITLE | STATUS | TRADE |
-------------------------------
| 1 | test1 | 1 | 1 |
| 2 | test2 | 1 | 1 |
| 3 | test3 | 1 | 0 |
| 4 | test4 | 0 | 1 |
The trade table
| ID | TRADER | ITEM | URL |
------------------------------------------------------
| 1 | 2 | 1 | HTTP://www.test.com/itemOne |
| 2 | 5 | 3 | HTTP://www.test.com/itemThree |
| 3 | 5 | 4 | HTTP://www.test.com/itemFour |
Say I want to have a list of all the items that are not being traded by trader 5 and have a status of 1. So when trader 5 comes to the site they will be able to select the remaining items to trade.
Here is what I have tried:
$sql = "SELECT m.id, m.title
FROM main AS m, trade AS t
WHERE m.trade >= 1 && m.status = 1 &&
t.trader <>". mysql_real_escape_string($traderID);
This code just doesn't work. Any ideas on this?
It is not clear to me what column in Trades is an FK to Main. Below, I have assumed it is the Item column:
select m.id, m.title
from Main m
where not exists (
select *
from trade
where m.id = item
and trader = 5
)
and m.status = 1
Try this:
SELECT id, title FROM main
WHERE status = 1 AND id NOT IN
(SELECT item FROM trade WHERE trader = 5);
This will grab a list of every title in main with a status of 1, but limit the items based on a subquery which gets a list of ids already traded by trader 5 (i.e. items "not in" the list of items returned as having been traded by trader 5).
I'll leave it to you to update the query to be parameterized as needed.
Note that I'm assuming that item in trade is a foreign key to the id field in main, since you didn't specify it.