mysql select with 2 different substrings and single WHERE from 1 column - mysql

i am trying to pull data from sql with several conditions from a single column
how can i get this to work?
SELECT RIGHT(productID, 2) AS 'a', SUBSTRING(productID,1,2) AS 'b', productID
FROM products
WHERE `group`='$one'
AND `a` LIKE 'AA%'
AND `b` LIKE '$two'
i am trying to get the first 2 letters of the row and last 2 letters from the same row as well as checking if group=$one but get this error
Unknown column 'b' in 'where clause'

SELECT RIGHT(productID, 2) AS 'a', SUBSTRING(productID,1,2) AS 'b', productID
FROM products
WHERE `group`='$one'
GROUP BY productID
HAVING
a = 'AA'
and b LIKE '$two'
No need for the like it's two positions % increases execution.

I created a simple table (sotest for StackOverFlow Test table) and added a VARCHAR column to it called col1. The code below worked for me.
SELECT RIGHT(col1, 2) AS a, SUBSTRING(col1,1,2) AS b, col1 as col FROM sotest Having a like 'in%' and b like 'te%'
The return is as bellow
| a | b | col |
|'in'| 'te'| 'test_jermin'|

SELECT RIGHT(productID), 2)
^---extra bracket
you're terminating the function before you specify how many characters to extract.

That is because of your error in RIGHT function call, you have a misplaced parenthesis. Change the statement of SELECT to this:
SELECT RIGHT(productID, 2)

Related

MySQL query - condition (comparing strings) does not work

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%'.

Sort columns based on data in tuples mysql

Say, I have a table
A B C D E F
1 2 4 3 6 5
4 2 3 1 6 5
4 5 3 6 1 2
How can one get an output based on rearranging based on its data. For example,
ABDCFE
DBCAFE
EFCABD
is it possible?
EDIT:
The question seems to be asking: How can I get the list of column names in order by value?
I got it. You want to sort the values in each row and show the names of the columns in order.
Let me assume that you have a row id, so you can identify each row. Then:
select id, group_concat(which order by val) as ordered_column_names
from ((select id, a as val, 'A' as which from t) union all
(select id, b, 'B' as which from t) union all
(select id, c, 'C' as which from t) union all
(select id, d, 'D' as which from t) union all
(select id, e, 'E' as which from t) union all
(select id, f, 'F' as which from t)
) t
group by id
order by id;
SQL is fundamentally not the tool to do the operation you describe, because it violates the concept of a relation. I don't mean the common use of "relation" meaning a relationship, I mean the mathematical definition of relation.
There is no order of columns in a relation. The columns are a set, which is by definition unordered. Columns are identified by their name, not their position left-to-right.
All the entries in rows under each respective named column must be part of the same data domain. If you mix them around on a row-by-row basis, you're violating this.
I guess all your columns A through F are actually using values in the same data domain, or else reordering them wouldn't make any sense. If this is true, then you're violating First Normal Form by defining a table with repeating groups of columns. You should instead have all six columns be in one column of a second table. Then it becomes very easy to sort them by value.
Basically, what you're trying to do is better solved by formatting the data results in some application code.
There is a way to do it ,get coulmn name by column ordinal order and print it.
For each value in coulmn iterate this and get the column name for the ordinal specified in cloumn data. Here ordinal position is value in each coulmn data. Iterate for each row and each column and your problem is solved.
select column_name
from information_schema.columns
where table_name = 'my_table_name' and ordinal_position = 2;
It appears that you are asking for output where each row in the output is just a specification of the order of the data values in the columns.
Then, if the values are always integers between 1 and 5, you can do it by outputting a character value of 'A' where the data value is 1, a 'B' where the data value is 2, etc. This SQL will do that.
Select Char(A+64)A, Char(B+64) B,
Char(C+64) C, Char(D+64) D,
Char(E+64) E, Char(F+64) F
From table
if the want the column sort order in one output column, you could also do this:
Select Char(A+64) + Char(B+64) +
Char(C+64) + Char(D+64) +
Char(E+64) + Char(F+64) SortOrder
From table

Union table where column matches defined varchar

I'm trying to merge 2 tables x and y having the same columns inside both, But I just want to find just 'EMPTY' lines on column name from both tables and add extra column as Source to know from what table the line came from.
The column name is varchar(128), I tried to find by NULL instead of 'EMPTY' and did not work, might be better to find NULL since I add an extra query just to set 'EMPTY' where is NULL
This is what I currently have:
SELECT data,name,id,'Friends' as Source FROM droid_friends union all SELECT data,name,id,'Followers' as Source FROM droid_followers WHERE name = 'EMPTY'
if you want use NULL your query should be:
SELECT data,name,id,'Friends' as Source FROM droid_friends union all SELECT data,name,id,'Followers' as Source FROM droid_followers WHERE name IS NULL
Seems like this is the solution
SELECT * FROM ( (SELECT data,name,id,'Friends' as Source FROM droid_friends WHERE name='EMPTY' )UNION(SELECT data,name,id,'Followers' as Source FROM droid_followers WHERE name='EMPTY' )) as combined

What is this SQL query syntax mean?

SELECT b.*,
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1
From this sql syntax, what is "," which after "*" mean?
and anyone can explain this query to me or tell me where I can start?
It means all the columns from table Booking, and to the far right (the last column per row) bring in the user_name column from table user relating on the user.user_id matching the booking.booking_inspector. Such that the Booking.confirm is 1, and Booking_inspector is filled in with a parameter passed.
So it limits the rows of output to confirm is 1 and Booking_Inspector is the parameter passed (or bound, etc) depending on the language calling it.
Select * means all columns. So all columns from the one table, and just one column from the other
In this case, (select a.USER_NAME from A.db.USER a where a.USER_ID=b.Booking_Inspector) is the subquery which will return column a.USER_NAME. So this query is selecting everything from b (b.*) and the column a.USER_NAME from the subquery. So like you put comma between column names in select query, it is same.
select all columns from b and one more column from that subquery as USER_NAME.
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
That whole thing above as 1 column
SELECT b.*, [USER_NAME]
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1

Mysql SELECT and return multiple rows, but prefer one column value over another when present

So i'm looking to write a MySQL query that will return a result set that, when a particular column has a particular row value, it will return that row instead of another near duplicate row but otherwise return results like normal.
Okay, here is my table
id name value another
1 name1 value1
2 name1 value1 foo
3 name2 value2
4 name3 value3
and results should be (if foo is present):
id name value another
2 name1 value1 foo
3 name2 value2
4 name3 value3
I did find this example: MySQL get rows but prefer one column value over another but couldn't figure out how to adapt it to my needs...
I hope I'm making sense! No sleep in two days ain't good for attempts at elucidation! Also I'm very sorry if this has already been asked, i searched for a good long time but just didn't have the vocabulary to find any results...
Thanks in advance!
SELECT
a.*
FROM atable a
LEFT JOIN atable b ON a.name = b.name AND a.another = 'foo'
This will filter out rows with an empty another, for which an entry with the same name and value exists that does have another.
select *
from YourTable yt1
where not exists
(
select *
from YourTable yt2
where yt1.id <> yt2.id
and yt1.name = yt2.pname
and yt1.value = yt2.value
and yt1.another = ''
and yt2.another <> ''
)
This sounds like a situation where the mysql coalesce function would be handy.
Coalesce returns the first non-null parameter it's given. So you can use,
SELECT id, COALESCE(another, value) FROM MyTable;
this will return two columns, the id field and either the contents of the "another" column (if it is not null) or the contents of the "value" column.