I have a table setup with the following rows:
orderfrom | Name
-------------------------------------------------------
ClearVision, Oakley, I-Deal Optics | ClearVision
ClearVision | I-Deal Optics
ClearVision | Oakley
I want a query that will select rows where the "Name" value is not found in the "orderfrom" column. In this case, it would return the 2nd and 3rd rows, but not the first row (since "ClearVision" is in the orderfrom column).
Here is what I've tried:
SELECT * FROM `dist`
JOIN `patientacct`
ON `patientacct`.dist LIKE CONCAT('%', `dist`.name ,'%')
INNER JOIN `treasure`
ON `treasure`.`id` = `patientacct`.`id`
WHERE (status = 'To Order' AND ****** ORDER BY `name` ASC);
The ** part is where I need help. I have tried this:
(( `dist`.name NOT LIKE CONCAT('%',`patientacct`.orderfrom,'%')) ))
INNER JOIN treasure
ON treasure.id = patientacct.id
WHERE
status = 'To Order'
AND dist.name NOT LIKE CONCAT('%',patientacct.orderfrom,'%')
I am not sure, but wouldn't this much simpler code work just fine?
mysql> select * from stringComp;
+------------------------------------+---------------+
| value1 | value2 |
+------------------------------------+---------------+
| ClearVision, Oakley, I-Deal Optics | ClearVision
|
| ClearVision | I-Deal Optics |
| ClearVision | Oakley |
| ClearVision, Oakley, I-Deal Optics | ClearVision |
+------------------------------------+---------------+
4 rows in set (0.00 sec)
Note the row 1 value 2 has extra whitespace around it.
mysql> select * from stringComp where value1 not like concat('%',value2,'%');
+------------------------------------+---------------+
| value1 | value2 |
+------------------------------------+---------------+
| ClearVision, Oakley, I-Deal Optics | ClearVision
|
| ClearVision | I-Deal Optics |
| ClearVision | Oakley |
+------------------------------------+---------------+
3 rows in set (0.00 sec)
This simple SQL compares the contents of column value2 to see if they are in column value1 and returns the rows where they are not.
So, to answer your question, I think this little snippet in the where clause shold get you the right results: value1 not like concat('%',value2,'%')
Edit: I just saw the extra line in your query: I think there is an extra bracket in
(( `dist`.name NOT LIKE CONCAT('%',`patientacct`.orderfrom,'%')) ))
Should be:
(( `dist`.name NOT LIKE CONCAT('%',`patientacct`.orderfrom,'%')))
Edit 2: Why do you have all those brackets in there anyhow?
SELECT
*
FROM
`dist`
JOIN `patientacct`
ON `patientacct`.dist LIKE CONCAT('%', `dist`.name ,'%')
INNER JOIN `treasure`
ON `treasure`.`id` = `patientacct`.`id`
WHERE
status = 'To Order'
AND `dist`.name NOT LIKE CONCAT('%',`patientacct`.orderfrom,'%')
ORDER BY
`name` ASC;
Related
why I can't Select a row of a table with an unequal concat construction? I'll show you an example.
Table1
| id | area |
| 1 | items_labeling_small |
| 2 | items_labeling_big |
Table2
| id | area | kat |
| 1 | small | labeling |
| 2 | big | labeling |
SELECT Table1.area FROM Table1, Table2 WHERE Table1.area != CONCAT('items_', Table2.kat, '_', Table2.area)
No results have to be shown, because both are matching with the concat construction. But they're shown in result. I've no idea why.. and how I can change the query that it works.
They are show because each row don't match the other so you have the rows that not match
SELECT Table1.area
FROM Table1
INNER JOIN Table2 ON Table1.area != CONCAT('items_', Table2.kat, '_', Table2.area)
could be you want a not in
SELECT Table1.area
FROM Table1
where Table1.area NOT IN (
SELECT CONCAT('items_', Table2.kat, '_', Table2.area)
FROM Table2
)
and as a suggestion you should not use the (old) implict join sintax based on where clause ...use explicit join sintax ..
I have a table where it contains user details. Now I need to execute a MySQL query which satisfies the below conditions:
There are two tables
1) user table - which contains userid,name,firstname
2) category table - which containts categoryid, category type, assinged userid
Now I want to execute the query which satisfies the below condition
A User can only sees a category with a type of simple,complex and which are assigned only to his userid and that category shouldn't get assigned to any another userid.
Anyone please help me in this.
drop table if exists category;
create table category (id int, categorytype varchar(10), assignedUserId int);
truncate table category;
insert into category values
(1,'simple',1),
(2,'complex',1),
(3,'simple',2),
(4,'complex',3),
(5,'odd',1);
MariaDB [sandbox]> select * from users where id < 6;
+----+----------+----------+--------+---------------------+
| id | userName | photo | status | ts |
+----+----------+----------+--------+---------------------+
| 1 | John | john.png | 1 | 2016-12-08 13:14:24 |
| 2 | Jane | jane.png | 1 | 2016-12-08 13:14:24 |
| 3 | Ali | | 1 | 2016-12-08 13:14:24 |
+----+----------+----------+--------+---------------------+
3 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select u.*, c.*
-> from users u
-> join category c on c.assigneduserid = u.id
-> where u.id = 1
-> and c.categorytype not in (select c.categorytype from category c where c.assigneduserid <> 1);
+----+----------+----------+--------+---------------------+------+--------------+----------------+
| id | userName | photo | status | ts | id | categorytype | assignedUserId |
+----+----------+----------+--------+---------------------+------+--------------+----------------+
| 1 | John | john.png | 1 | 2016-12-08 13:14:24 | 5 | odd | 1 |
+----+----------+----------+--------+---------------------+------+--------------+----------------+
1 row in set (0.00 sec)
Try this:
SELECT *
FROM category c
where userid = 123
and not exists (
select 1
from category c2
where c.userid <> c2.userid
and c.categorytype = c2.categorytype
);
Note that this query doesn't require the id to be mentioned at two places unlike the other answer, and hence is a more general one.
SELECT * FROM categories
WHERE category_type = 'simple' OR
category_type = 'complex' AND // (edited after comment of Philipp)
assinged_userid = (Your User ID Here)
How to join many fields with different ID's in only one?
I have this MySQL table:
--------------------------------
| *UDH* | *Text* |
--------------------------------
| 050003B90301 | Hi my name is A|
--------------------------------
| 050003B90302 | rmin and I wan |
--------------------------------
| 050003B90303 | t be your frien |
--------------------------------
The UDH field is different but I need join the text field to copy to other table, the result must to be like this:
______________________________________________________________
| UDH | Text |
--------------------------------------------------------------
| 1 | Hi my name is Armin and I want be your frien |
---------------------------------------------------------------
Do you know a PHP sentence or other method to make something like this?
Use GROUP_CONCAT(). Like this:
select '1' as UDH, group_concat(`Text` separator '') as `Text` from myTable;
If you need to order by the UDH column, you can do that within the group_concat() call like this:
select '1' as UDH, group_concat(`Text` order by UDH separator '') as `Text` from myTable;
I just validated this query with the following test code based on your example:
mysql> create table myTable (UDH varchar(32), `Text` text) engine=innodb;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into myTable (UDH, `Text`) values ('050003B90301', 'Hi my name is A'), ('050003B90302', 'rmin and I wan'), ('050003B90303', 't be your frien');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from myTable;
+--------------+-----------------+
| UDH | Text |
+--------------+-----------------+
| 050003B90301 | Hi my name is A |
| 050003B90302 | rmin and I wan |
| 050003B90303 | t be your frien |
+--------------+-----------------+
3 rows in set (0.00 sec)
mysql> select '1' as UDH, group_concat(`Text` order by UDH separator '') as `Text` from myTable;
+-----+----------------------------------------------+
| UDH | Text |
+-----+----------------------------------------------+
| 1 | Hi my name is Armin and I want be your frien |
+-----+----------------------------------------------+
1 row in set (0.01 sec)
In a table myTable defined as:
+----+---------+-----------+
| id | name | value |
|----+---------+-----------+
| 7 | hand | right |
| 5 | hand | left |
| 0 | hand | both |
| 0 | feet | both |
| 0 | eyes | green |
| 9 | eyes | blue |
| 2 | eyes | white |
| 2 | hand | raised |
+----+---------+-----------+
Default settings are controlled by id = 0.
My question is how to write a select statement to get name,value for id = 5 in one query that will include set for id = 5 and any defaults not overridden.
The results should be:
+---------+-----------+
| name | value |
+---------+-----------+
| hand | left |
| feet | both |
| eyes | green |
+---------+-----------+
It isn't clarified if the ordering of the result set is important, so might as well try:
SELECT name,
value
FROM MyTable
WHERE id = 5
AND id NOT IN
(
SELECT id
FROM MyTable
WHERE id = 0
GROUP BY MyTable.id
)
UNION
SELECT name,
value
FROM MyTable
WHERE id = 0
AND name NOT IN
(
SELECT name
FROM MyTable
WHERE id = 5
GROUP BY MyTable.name
)
Disclaimer: Tested in SQL Server, but not using anything specific to that version of SQL.
seems this one may work for you:
SELECT id,name,value
FROM test
WHERE id = 5
UNION
SELECT id,name,value
FROM test
WHERE id=0
and name not in (select name from test where id=5)
The following does not return three rows in the result set, it only returns a single row with columns hand, feet and eyes, so it may not work for you. But it should return the data that you're looking for given your conditions:
SELECT (CASE
WHEN EXISTS(SELECT 1 FROM myTable WHERE name='hand' AND id=5)
THEN (SELECT `value` FROM myTable WHERE name='hand' AND id=5)
ELSE (SELECT `value` FROM myTable WHERE name='hand' AND id=0)
END) AS hand,
(CASE
WHEN EXISTS(SELECT 1 FROM myTable WHERE name='feet' AND id=5)
THEN (SELECT `value` FROM myTable WHERE name='feet' AND id=5)
ELSE (SELECT `value` FROM myTable WHERE name='feet' AND id=0)
END) AS feet,
(CASE
WHEN EXISTS(SELECT 1 FROM myTable WHERE name='eyes' AND id=5)
THEN (SELECT `value` FROM myTable WHERE name='eyes' AND id=5)
ELSE (SELECT `value` FROM myTable WHERE name='eyes' AND id=0)
END) AS eyes
;
Output:
+---------+-----------+-----------+
| hand | feet | eyes |
+---------+-----------+-----------+
| left | both | green |
+---------+-----------+-----------+
This is approach generates 10 different SELECT statements rather than 1, and the accepted answer is, for most applications, probably a better way to go about it.
SELECT * FROM mytable WHERE ID = 5
UNION
SELECT * FROM mytable WHERE ID = 0 AND name NOT IN (SELECT name FROM MyTable
WHERE id = 5)
Should get you the right answer.
http://sqlfiddle.com/#!9/1f516/14
I having two tables
table 1: users
| id | username |
| 1 | john |
| 2 | marry |
| 3 | deep |
| 4 | query |
| 5 | value|
and
table 2:users_2
| table_2_id | user_id |
| 1 | 2,4 |
I need required something like this
| table_2_id | username |
| 1 | marry,query |
anyone can help me for this output in mysql
Is this what you are looking ?
select
`users_2`.`table_2_id` , GROUP_CONCAT(`users`.`username`) as `usernames`
from `users_2`
inner join `users` on FIND_IN_SET(`users`.`id`,`users_2`.`user_id`)
Check output here
http://sqlfiddle.com/#!2/c498bc/3
select a.table_2_id,b.username
from users b,users_2 a
where a.table_2_id=b.id
and b.id in(a.user_id)
group by a.table_2_id
First of all, you should not store a multiple value in a single field. For table users_2, the data should be:
table_2_id user_id
1 2
1 4
After you normalized your table, you can use mysql GROUP_CONCAT() to get the result in the format you mentioned
SELECT
users_2.table_2_id,
GROUP_CONCAT(users.username) AS username
FROM
users_2
JOIN
users ON users.id = users_2.user_id
GROUP BY
users_2.table_2_id
;