In mySql When I search with id and last name I see no results. Below
SELECT * FROM TableName
WHERE id = '-2105192515' AND last_name='JOHNSON'
0 Results ----- (id = mzb_indiv_id)
But when searching with only Id I get one record match. Below is the query
SELECT * FROM TableName
WHERE id = '-2105192515'
1 Record match . --- (id = mzb_indiv_id)
Also when searching with last_name I see the results too. But why is there a difference between the above two searches?
The first query only returns a row if it has that id and also has that last_name, in the same row.
(It does not mean : rows with this id and also rows with that last name.)
I guess the result from your second query doesn't have the last_name JOHNSON?
Perhaps you want an OR rather than an AND?
Edit
Your example data has ' JOHNSON' rather than 'JOHNSON' (an extra space at the start)
Try last_name LIKE '%JOHNSON%' or TRIM(last_name) = 'JOHNSON'
From your question, first thing I found
SELECT * FROM TableName
WHERE id = '-2105192515'
AND last_name='JOHNSON'
Here column name is 'last_name' but in in info below you wrote column name 'last_Name'.
Check your column names, as the missing column would give error in executing, so it is highly unlikely.
Second thing: are you spelling 'JOHNSON' the same way it is stored in your database? no spaces included?.
Please, post the result set with both queries to get a proper, and more valid answer.
The row which id is -2105192515 has a last_name column value which is not equal to JOHNSON
SELECT * FROM TableName
WHERE id = '-2105192515' AND last_name='JOHNSON'
This query means - return all the rows that their id = '-2105192515' AND the last_name='JOHNSON'
You may have wanted to use OR instead?
Related
My table has only got 1 Column 'Name' with 100 unique entries.
I need to find out if a given Value exists in that table.
I am using:
SELECT 1 FROM `tbl_names` WHERE `Name` = "Lisa"
MySQL returns an empty result, so no 0 for not found and no 1 for found, even though the given name is an entry in that table.
What am I missing here?
select count(*) from tbl_name where name = 'Lisa' - will return count of entries with Lisa in the column. You can do as before with select 1, and calculate results - zero size means no occurance
If you want to return as 0 or 1, I would suggest:
select (exists (select 1 from tbl_names where Name = 'Lisa')) as flag
This will not fix the problem that you describe -- but it will always return one row with a single column whose value is 0 or 1.
'Lisa' is not in the table. You might try where Name like '%Lisa%'.
I have one such sql:
select name from A where id in (23,24,22,23)
When I run it in Navicat, the result only have one result of 23.
My question is, how to keep the number and order of the query results remains the same as (23,24,22,23).
If you want to maintain the order of the result then use order by clause like
select name from A
where id in (23,24,22)
order by id;
Again, assuming that id is a primary key column in your table A then there will be only one row with id = 23. How do you expect the same row to get repeated automatically unless you make it explicit by using a UNION ALL
If you really really want to fetch the records like this, you can use field function to get 23,24,22 and order by this sort:
select name from A where id in (23,24,22) order by field(id, '23,24,22')
then use union all get another 23:
(select name from A where id in (23,24,22) order by field(id, '23,24,22'))
union all
select name from A where id = 23
I want the result in a such a way that it should return the all employee details if the value which I am going to mention in where clause is in different column.
so for that i have write my query like:-
select *
from claim_master
where full_name='Trevor DSouza' in (select * from claim_master);
i.e I want the query should show the result if 'Trevor DSouza' name is exist in different column.
but it shows the result as:-
Error Code: Operand should contain 1 column(s)
please help me to solve my error.please
you can try this
you have to mention the column name in the query it will not work like the above you have written. You can use the below snippet and try:
select
*
from
claim_master
where full_name like 'Trevor DSouza'
OR last_name like 'Trevor DSouza'
OR other_column_name like 'Trevor DSouza';
just add the columns name in or conditions.
Is there any way to do something like :
SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;
?
No, you can't. Column order doesn't really matter in MySQL. See the below question for more details.
mysql - selecting values from a table given column number
If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.
I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.
We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.
How I did it:
I'm trying to take (last 3 values of) column number 4 in sometable.
set #mydb=(SELECT DATABASE());
set #mycol=(select COLUMN_NAME from information_schema.columns where
table_schema=#mydb and table_name='sometable' and ordinal_position = 4);
SELECT Date,#mycol FROM sometable ORDER BY Date DESC LIMIT 3;
Of course, if Database name is known, first line could by whiped and #mydb replaced by real database name.
You can do this trick
Example:
$query="select * from employee";
$result=mysql_query($query);
$meta=mysql_fetch_field($result,0) // 0 is first field in table , 1 is second one ,,, etc
$theNameofFirstField=$meta->name; // this well return first field name in table
// now you can use it in other query
$seconQuery="select $theNameofFirstField from employee";
I want to get the exact row from the following data
id name groupid
1 robert 1,2
2 henry 11,12
My query is
SELECT * FROM table WHERE groupid LIKE '%1%'
Above query will return both row
How to get the first row ?
You could use FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('1', groupid)
But as suggestion, you should not save data like this.
assuming groupid is a varchar column having ids stored as comma seperated list you can try this:
select * from table where CONCAT(',',groupid,',') LIKE '%,1,%';
or better approach would be to use FIND_IN_SET function in mysql:
select * from table where find_in_set(1, groupid);
To get an exact row from a database table, just specify all the fields:
SELECT *
FROM myTable
WHERE id = 1
AND name = 'robert'
AND groupid = '1,2'
Or, assuming id is the unique primary key, you can just use that:
SELECT *
FROM myTable
WHERE id = 1
Using LIKE '%1%' will return all the results which contain 1. Use #lc solution WHERE groupid = '1,2' to get only result with that specific id.