I got table called 'pet' have the following values
id | name | nickname | dateofborn |
1 | test | jhon | 2009 |
2 | test2| test | 2010 |
3 | mike | NULL | 2010 |
3 | jhon | testor | 2011 |
I want to select all of columns that contain 'test' value either in name column or nickname column so i have this query didn't actually work for me:
SELECT * FROM pet WHERE name='test' OR nickname= 'test'
note that I exactly want test value not testor.
The query is right, I think you have some problems with your Table.
But try like below:
USE [yourDB es: animals];
SELECT * FROM [yourDB].pet WHERE (name='test' OR nickname='test');
try this.
SELECT * FROM pet WHERE name like 'test%' OR nickname like 'test'
Related
I have a table like this
+-----+------------------+
| id | name |
+-----+------------------+
| 1 | John;Black;Mike |
+-----+------------------+
| 2 | White;Mike;John |
+-----+------------------+
| 3 | Jacob;Mike |
+-----+------------------+
| 4 | Will;Mason;Mike |
+-----+------------------+
as result of
SELECT * FROM people WHERE name LIKE '%Mike%';
Is there any query on how to update specific name Mike to Michael without updating a whole value. like John;Black;Mike to John,Black,Michael in all rows automatically.
You could use replace
update people
set name = replace( name, 'Mike', 'Michael')
where name LIKE '%Mike%';
anyway you should avoid storing comma separated value .. you should think to a proper normalized table for this data ..
I have a table with this sort of data:
+------------+----------+----------+
| Unique ID | Name | Class |
+------------+----------+----------+
| 1 | Name 1 | Class A |
| 2 | Name 2 | "" |
| 3 | Name 3 | Class C |
| 4 | Name 1 | "" |
| 5 | Name 4 | "" |
| 6 | Name 4 | "" |
+------------+----------+----------+
I am trying to do something I thought was simple, but i did not find so.
I am trying to "extract" only the lines with an empty string value in 'Class' for a group of equal names.
So in this case I would get this result :
+------------+----------+--------+
| Unique ID | Name | Class |
+------------+----------+--------+
| 2 | Name 2 | "" |
| 5 | Name 4 | "" |
+------------+----------+--------+
So not Name 1 because even though there is a line with "" there is another line with 'Class A'.
I thought a UNION would do the job but I am not gettgin anything because I think unions are for two tables but the problem here is I have the data in the same table.
Thank you for your help
Access syntax may be a bit different but this returns what you want in Oracle:
SELECT distinct Name, Class FROM table1 Where Name NOT in (select name from table1 where class is not null)
A Union melds two result sets, whether or not they come from the same table is irrelevant. What you want to do is omit from the result set the rows with the same name AND class is not null. Not having your query to expand on or change is a problem, but if you add a clause that says something like where "name not in (select name from table where class is not null);", that may do it.
I have data stored in a mySQL database in the following format:
+------------+------------+-----------+
| id | field | value |
+============+============+===========+
| 1 | first | Bob |
+------------+------------+-----------+
| 1 | last | Smith |
+------------+------------+-----------+
| 2 | first | Jim |
+------------+------------+-----------+
| 2 | last | Jones |
+------------+------------+-----------+
and I would like it returned as follows:
+------------+------------+-----------+
| id | first | last |
+============+============+===========+
| 1 | Bob | Smith |
+------------+------------+-----------+
| 2 | Jim | Jones |
+------------+------------+-----------+
I know this seems like a silly way to store data, but it's just a simple example of what I really have. The table is formatted this way from a WordPress plugin, and I'd like to make it work without having to rewrite the plugin.
From what I've read, I can't use PIVOT with mySql. Is there something similar to PIVOT that I can use to achieve what I'm going for?
Try this pivot query:
SELECT id,
MAX(CASE WHEN field = 'first' THEN value ELSE NULL END) AS first,
MAX(CASE WHEN field = 'last' THEN value ELSE NULL END) AS last
FROM yourTable
GROUP BY id
Follow the link below for a running demo:
SQLFiddle
Try this;)
select
id,
max(if(field='first', value, null)) as first,
max(if(field='last', value, null)) as last
from yourtable
group by id
SQLFiddle DEMO HERE
Let's say we have the table PEOPLE like
id | name | extracted
-----------------------------------------
1 | Roger |
-----------------------------------------
2 | Anthony |
-----------------------------------------
3 | Maria |
-----------------------------------------
We use
SELECT SUBSTRING(name, 1, 3) FROM people.name WHERE name like '%thon%'
It will find "Anthony" and extract 3 first chars - so result is :
Ant
How to put this result against same id so the table looks like
id | name | extracted
-----------------------------------------
1 | Roger |
-----------------------------------------
2 | Anthony | Ant
-----------------------------------------
3 | Maria |
-----------------------------------------
Try
UPDATE people SET extracted = LEFT(`name`,3) WHERE `name` like '%thon%'
ID | First Name | Last Name |
-----------------------------
1 | Test | NULL |
2 | Test | ABC1 |
I need to merge these two rows into one to display as so the null in 'last name' will be replaced by the text in the second column, Grouping by the first name.
ID | First Name | Last Name |
-----------------------------
1 | Test | ABC1|
try this:
select min (id),First_Name,MAX(Last_Name)
from your_table
group by First_Name