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
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.
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
Say i have this table :
---------+----------
name | id
---------+----------
apple | 1
orange | 2
book | 3
notebook | 4
textboo | 5
phone | 6
if the user want to sort this table as he want ..
not order them as id or name .. order them as he want to show them ..
How to make something like that ?
Can we do them by java or jquery or something else
if user want to display orange | 2 then phone | 6
then the other values or want to display notebook | 4 first then other values how we can control something like that ?
Suppose you have multiple users you will need to create a new table where you will save each user's sort order preferences.
---------+-------------------
item_id | user_id | sort
---------+------------------
1 | 1 | 2
2 | 1 | 1
3 | 1 | 3
After that you can easily use ORDER BY in your query to fetch the order chosen by the user based on his user_id. If you're looking for user_id = 1, the following query should work:
SELECT * FROM `table1` t1
LEFT JOIN `preferences_table` PT ON PT.item_id = t1.id
WHERE user_id = 1
ORDER BY PT.sort ASC
Let's say I have three tables like these:
table name:
id | name
1 | Bob
2 | Alice
3 | Bryan
etc
table pants:
id | size
1 | S
3 | M
table skirt:
id | size
2 | M
How do I merge the three tables using MySQL and obtain a table like this:
result table:
id | name | pants | skirt
1 | Bob | S |
2 | Alice| | M
3 | Bryan| M |
When there's no matching id, the cell will just be blank.
Join the tables with a left join. Like this:
SELECT
tablename.id,
tablename.name,
tablepants.size AS pants,
tableskirt.size as skirt
FROM
tablename
LEFT JOIN tablepants
on tablename.id=tablepants.id
LEFT JOIN tableskirt
ON tablename.id=tableskirt.id
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.