I have an array ('abc','def','ghi','jkl'). I want to find the values which are not in mysql table.
That is, if 'def'is not in the table, then it should show 'def' and so on.
My query was:
SELECT column FROM table WHERE column not in ('abc','def','ghi','jkl')
But its wrong. How can I get the values which are not there in the column?
You should put these values first in some table and then do "Not in" like :
SELECT column FROM table WHERE column not in (select distinct col1 from table1).
Here is how you can do it:
select x.col
from (values row('def'),row('abc'),row('ghi')) x(col)
left join table t on t.col = x.col
Where t.col is null
Related
I have column in table1 where name column has lot of rows as null
table1.name=table2.userID
So I m trying to put IFNULL condition as this
select (IFNULL(name,'empty'))
But My output table after exceution comes blank.
It should work
SELECT IFNULL(`name`, 'empty') FROM `tablename`
I want to have a SELECT statement name columns based on other column values.
Let's say I have a table with column names like q_1, q_2 and other columns like q_1_name and q_2_name
Right now we are doing something like
SELECT SUM(q_1), SUM(q_2) from mytable;
I'd like to get a result set with the columns named for the values in q_1_name and q_2_name
SELECT SUM(q_1) as (q_1_name), SUM(q_2) as (q_2_name) from mytable;
Any chance you know a way to do this?
You can use a simply alias AS
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable;
or using a subselect
select t.q_1_name, t.q_2_name
from (
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable
) t;
I'm using PDO with MySQL.
I want to select all rows from a given table with distinct values in a given column, but SELECT DISTINCT column_name FROM table returns the rows with only that column_name. Therefore I can't access the other row's columns.
I've been searching for answers and it looks like SELECT DISTINCT column_name FROM table is supposed to return all the rows with distinct values inside column_name with all the row's columns. However, I only get the column I want distinc'ed:
Array
(
[image] => leather_helmet.jpg
// there are supposed to be more fields here...
)
May this be a PDO's bug or am I doing something wrong?
Thanks in advance! :)
If you want only 1 column distinct you have to think of which record you want for the other columns. For instance if you like the min id record for the distinct column then you can do
SELECT *
FROM armor_unsealed
WHERE id IN
(
SELECT min(id)
FROM armor_unsealed
WHERE piece=:piece
GROUP BY image
)'
I have a simple table with two columns. The first field represents an id, the second one a name (string value). No field is unique. So, e.g., there are many records like:
What I need is a simple SQL statement which shows me the whole table in the following format and inserts the content into a new table.
Any help?
You have to use group by:
insert into tab2 (name,cnt)
select name, count(1) as cnt
from tab
group by name
Here is more informations about aggregate functions.
SQL Fiddle DEMO
Just use COUNT function to count from_id. And if you want to count in group, use GROUP BY clause for that. Like this one.
SELECT `name`, COUNT(from_id) AS `COUNT`
FROM myTable
GROUP BY `name`;
This will return your desired result.
Now you want to insert it into new table then you can do like this:
INSERT INTO newTable (`name`, `count`)
SELECT `name`, COUNT(from_id) AS `COUNT`
FROM myTable
GROUP BY `name`;
I have two database columns in a mysql database, A and B.
In a select query, I want to implement this logic:
Select the rows where A is 'X'.
If A is not set in a row, then check and select the row only if column B='Y'.
So one can say that column B is a fallback for column A.
How could I construct a SELECT query with 'X' and 'Y' as inputs for the WHERE clause?
Use boolean logic:
SELECT *
FROM table
WHERE A = 'X' OR (A IS NULL AND B = 'Y')
I think this should work:
SELECT * FROM table
WHERE (A='X') OR ((A IS NULL) AND (B='Y'))
SELECT * FROM table WHERE A='X' OR (A IS NULL AND B='Y')