MySQL not equal, not in does not work - mysql

I have a table (type is tinyint(1)):
id | name | type
-----------------
1 | John | 1
2 | Peter | 1
3 | Bob | 2
After calling a SELECT * FROM user WHERE type <> 1 I get 0 rows. Bob's line should have been returned.
I've tried NOT IN (1), != 1, but no success.

All the 3 type of queries Works in the following fiddle:
Whether it is int or text type, the query works:
Checkout the fiddle:
http://sqlfiddle.com/#!2/d4bb1/1
and
http://sqlfiddle.com/#!2/9b070/2

Related

MySQL not retrieving data when using an OR clause on VARCHAR type

Real Final edit (since it seems like people think is a null :P):
Let me rephrase it, since it is hard to explain and it seems no one can help me. I made 2 stored procedures in MySQL with phpMyAdmin. Both with an IN parameter VARCHAR(500) in utf8, var1.
With a value of 'novalue' for the In parameter these are the behaviours:
tableA:
------------------
a | b | example
------------------
1 | A | 1
2 | A | 1
3 | T | 1
SELECT * FROM tableA
WHERE (var1 = 'novalue')
SELECT * FROM tableA
WHERE (var1 = 'novalue' OR var1 = tableA.col1)
Expected output (Only first procedure will give me this result):
------------------
a | b | example
------------------
1 | A | 1
2 | A | 1
3 | T | 1
So my problem is, how to get the same expected output on the second procedure?
Thanks in advance

mysql order by date and sort parent table

i have this two table:
id | name
1 | Mike
2 | Jack
id | id_client | title | due_date | sort
1 | 1 | Xxxx | 2016-01-22 | 0
2 | 1 | Xxxx | 2016-01-24 | 1
3 | 2 | Xxxx | 2016-01-28 | 0
I need to order the first by date, and the second by sort. I have try this but not work:
->createQueryBuilder()
->select("a.*","b.*")
->from("table_a", "a")
->leftJoin("a", "table_b", "b", "a.id = b.id_client")
->addOrderBy('b.due_date', 'ASC')
->addOrderBy('b.sort', 'ASC')
Any help?
According to your example code you're trying to sort by 'due_date' from table B but the column's actually named 'date'. You don't say exactly what issue(s) you're having but I would start by resolving that mismatch - best to rename the column as 'date' is a reserved word in mySQL and using it may be an issue in itself.

ROR + MySql Query - To Compare Single Value With Comma Separated values present in Single Column

In Rails, I am trying one query for MySql - While searching data from DB related to hierarchy, I have to pass specific id on the table.
Table: Hierarchy
id | parent | name
1 | | Electronics
2 | 1 | iPhone
3 | 1 | Moto G ( Android )
4 | | Clothes
5 | 4 | Kidz Wear
Table: Comments
id | hierarchy_id | value
1 | 1 | Best electronic products values in this store.
2 | 1,2 | iPhone is always best.
3 | 4 | Cotton Clothes - Cool
4 | 1,3 | MotoG with Android M - Paise wasool
5 | 4,5 | New Collection Good One ...
Here, when I tried to search data using hierarchy 1, then it will show only one record. Here I am not getting the way to fetch remaining records, because if I select any parent hierarchy then CHILD data should be there.
And If I select any CHILD hierarchy, then system should return value only related to that CHILD, by escaping PARENT and SIBLINGS
Getting Right Now :-
$ select * from comments where hierarchy_id = 1;
id | hierarchy_id | value
1 | 1 | Best electronic products values in this store.
Expected Output has to be for hierarchy_id = 1 :-
$ select * from comments where **************************
id | hierarchy_id | value
1 | 1 | Best electronic products values in this store.
2 | 1,2 | iPhone is always best.
3 | 1,3 | MotoG with Android M - Paise wasool
Expected Output has to be for hierarchy_id = 3 :-
$ select * from comments where **************************
id | hierarchy_id | value
1 | 1,3 | MotoG with Android M - Paise wasool
Please suggest some thing ...
Since you have hierarchy_id in the comments table, why don't you use it in your query:
SELECT * FROM comments WHERE hierarchy_id=1

query to get most matched likes first in mysql

I have table like:
user :
uid | course_id | subjects
---------------------------
1 | 1 | html,php
2 | 1 | java,html,sql
3 | 1 | java
4 | 1 | fashion,html,php,sql,java
I want to run a query which can return most liked subjects in query and then second most and so on...
For Example :
select * from user where subjects like '%java%' or '%php%' or '%html%';
this query will return data like this:
uid | course_id | subjects
---------------------------
2 | 1 | java,html,sql
3 | 1 | java
4 | 1 | fashion,html,php,sql,java
but i want output like this :
uid | course_id | subjects
---------------------------
4 | 1 | fashion,html,php,sql,java
2 | 1 | java,html,sql
1 | 1 | html,php
3 | 1 | java
so the most matched subjects 1st then 2nd most matched subjects and so on....
Is there any modification in my query so that i can get this type of sorted output.
Never, never, never store multiple values in one column!
Like you see now this will only give you headaches. Normalize your user table. Then you can select normally.
It should look like this
uid | course_id | subjects
---------------------------
1 | 1 | html
1 | 1 | php
2 | 1 | java
2 | 1 | html
2 | 1 | sql
3 | 1 | java
...
or better introduce an new table subjects and then make a mapping table called course_subjects
subject
id | name
------------
1 | html
2 | sql
3 | java
...
course_subjects
uid | course_id | subject_id
---------------------------
1 | 1 | 1
1 | 1 | 2
...
Based on the way you want your results, it looks like you want to order by the number of subjects (or tags) within subject. This can be accomplished by counting the number of , (commas).
The way to count the number of occurances of a character is to subtract the original length by the length when the character is removed.
Example:
SELECT *
FROM USER
WHERE subjects LIKE '%java%'
OR '%php%'
OR '%html%'
ORDER BY ( Length(subjects) - Length(Replace(subjects, ',', '')) ) DESC;
SQLFiddle: http://sqlfiddle.com/#!2/cc793/4
Result:
UID COURSE_ID SUBJECTS
4 1 fashion,html,php,sql,java
2 1 java,html,sql
3 1 java
Note:
As juergen says it is a bad idea to store multiple values in one column.
With MyISAM storage engine you can do match against.
The simplest example:
SELECT *,
MATCH (subjects) AGAINST ('java php html') AS relevance
FROM `user`
WHERE MATCH (subjects) AGAINST ('java php html')
ORDER BY relevance DESC
In MySQL 5.6 full-text search is available with InnoDB too but needs a bit extra to make it work. For more info checkout the following post: http://www.mysqlperformanceblog.com/2013/03/04/innodb-full-text-search-in-mysql-5-6-part-2-the-queries/

Sort table records in special order

I have table:
+----+--------+----------+
| id | doc_id | next_req |
+----+--------+----------+
| 1 | 1 | 4 |
| 2 | 1 | 3 |
| 3 | 1 | 0 |
| 4 | 1 | 2 |
+----+--------+----------+
id - auto incerement primary key.
nex_req - represent an order of records. (next_req = id of record)
How can I build a SQL query get records in this order:
+----+--------+----------+
| id | doc_id | next_req |
+----+--------+----------+
| 1 | 1 | 4 |
| 4 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 0 |
+----+--------+----------+
Explains:
record1 with id=1 and next_req=4 means: next must be record4 with id=4 and next_req=2
record4 with id=5 and next_req=2 means: next must be record2 with id=2 and next_req=3
record2 with id=2 and next_req=3 means: next must be record3 with id=1 and next_req=0
record3 with id=3 and next_req=0: means that this is a last record
I need to store an order of records in table. It's important fo me.
If you can, change your table format. Rather than naming the next record, mark the records in order so you can use a natural SQL sort:
+----+--------+------+
| id | doc_id | sort |
+----+--------+------+
| 1 | 1 | 1 |
| 4 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 4 |
+----+--------+------+
Then you can even cluster-index on doc_id,sort for if you need to for performance issues. And honestly, if you need to re-order rows, it is not any more work than a linked-list like you were working with.
Am able to give you a solution in Oracle,
select id,doc_id,next_req from table2
start with id =
(select id from table2 where rowid=(select min(rowid) from table2))
connect by prior next_req=id
fiddle_demo
I'd suggest to modify your table and add another column OrderNumber, so eventually it would be easy to order by this column.
Though there may be problems with this approach:
1) You have existing table and need to set OrderNumber column values. I guess this part is easy. You can simply set initial zero values and add a CURSOR for example moving through your records and incrementing your order number value.
2) When new row appears in your table, you have to modify your OrderNumber, but here it depends on your particular situation. If you only need to add items to the end of the list then you can set your new value as MAX + 1. In another situation you may try writing TRIGGER on inserting new items and calling similar steps to point 1). This may cause very bad hit on performance, so you have to carefully investigate your architecture and maybe modify this unusual construction.