MySQL SELECT * WHERE column='value' from 2 tables - mysql

Edit2:
I like to get all values from 2 tables WHERE the value of highlight='1'
highligh is a column present in both tables. Both tables have an unique structure.
I want to get all data from both tables where highlight='1'
SELECT * FROM painting, instaview WHERE highlight='1'
...both give me the following error:
"Column 'highlight' in where clause is ambiguous"
I searched a lot and found a lot about this error but only complicated JOIN causes that are different than my case. i know the column is in both tables but I want the results of those 2 table queries joined.
Edit1:
As requested the structure of both tables:
Column Type Comment
id int(6) Auto Increment
category varchar(3)
filename varchar(30)
title varchar(200)
material varchar(200)
year varchar(4)
highlight tinyint(1)
active tinyint(1)

You need to say from which table, you are querying the highlight.
Can you try like this ?
SELECT * FROM painting JOIN instaview ON painting.highlight='1'

I did not know about the "qualify", but this joined my data in more columns, I just wanted the results of both tables. After more searching I found the keyword: UNION
SELECT * FROM painting WHERE highlight=1
UNION
SELECT * FROM instaview WHERE highlight=1
This did the trick!
(I like to excuse myself for not being very clear in my question, for me this was all very new)

If you want highlight='1' in both tables, and don't want to specify a table, you may use
SELECT *
FROM painting
JOIN instaview USING (highlight)
WHERE highlight='1'
The fields used in USING are "combined", and tablename qualification not needed.
PS. This is "bad practice" - specify tablenames for all fields when there are more than one table in whole query source (even when some fieldname is unique over data source - it is present in one table only).

Related

What is the new table name after joining two tables in sql?

I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;

Is there way to add multiple values to 1 ID in access

I have a table that has Act ID, and another table that has Act ID, percentage complete. This can have multiple entries for different days. I need the sum of the percentage added for the Act ID on the first tableZA.108381.080
First table
Act ID Percent Date
ZA.108381.110 Total from 2 table
ZA.108381.120
ZA.108476.020
ZA.108381.110 25% 5/25/19
ZA.108381.110 75 6/1/19
ZA.108381.120
ZA.108476.020
This would be generally considering not good practice. Your primary key should be uniquely identifiable for that specific table, and any other data related to that key should be stored in separate columns.
However since an answer is not a place for a lecture, if you want to store multiple values in you Act ID column, I would suggest changing your primary key to something more generic "RowID". Then using vba to insert multiple values into this field.
However changing the primary key late in a databases life may cause alot of issues or be difficult. So good luck
Storing calculated values in a table has the disadvantage that these entries may become outdated as the other table is updated. It is preferable to query the tables on the fly to always get uptodate results
SELECT A.ActID, SUM(B.Percentage) AS SumPercent
FROM
table1 A
LEFT JOIN table2 B
ON A.ActID = B.ActID
GROUP BY A.ActID
ORDER BY A.ActID
This query allows you to add additional columns from the first table. If you only need the ActID from table 1, then you can simplify the query, and instead take it from table 2:
SELECT ActID, SUM(Percentage) AS SumPercent
FROM table2
GROUP BY ActID
ORDER BY ActID
If you have spaces other other special characters in a column or table name, you must escape it with []. E.g. [Act ID].
Do not change the IDs in the table. If you want to have the result displayed as the ID merged with the sum, change the query to
SELECT A.ActID & "." & Format(SUM(B.Percentage), "0.000") AS Result
FROM ...
See also: SQL GROUP BY Statement (w3schools)

Nested SQL to find the values when one field is true

I have 2 tables, the first one of them is orderbook has a field namely
easy_order with datatype of tinyint(1) and the primary key is id.
The second table name is execution including the fields of buy_order_id and sell_order_id. These 2 fields are ref. from the id key from the orderbook table.
I would like to write a SQL to find all the rows from the execution table whose buy_order_id OR sell_order_id row from the orderbook table has the easy_order column value of 1.
I use MySQL database.
I would write it this way, avoiding repetition and dual nested selects:
select execution.*
from execution
inner join orderbook on
orderbook.id in (execution.buy_order_id, execution.sell_order_id) and
orderbook.easy_order = 1;
(You may prefer conceptually to put orderbook.easy_order = 1 in a where clause. It will produce the same results, so it's a matter of preference.)
I make it work The SQL is,
SELECT * FROM execution WHERE buy_order_id IN (SELECT id FROM orderbook WHERE easy_order=1)
OR
sell_order_id IN (SELECT id FROM orderbook WHERE easy_order=1);

How to compare two mysql row ids and extract stores which is not including

As you can see in the table above, we have two cultures, with cultureids of 4 and 5.
Each culture has some stores which are the same, and some have which are different, i.e. cultureid 4 has some stores that cultureid 5 does not.
How we can extract the stores which are not same in both cultures?
Well, first thing first, I think you should add your schema of the tables so we can provide you with better answers. More over the screenshot of the Query and the result beneath differ so we it is not very easy to understand the exact structure of your data. And you have an error in the Query - **c.**cultureid = **c.**cultureid, I think it should be s.cultureid = c.cultureid.
But any way, if we consider your nl_stores table looks like this:
CREATE TABLE IF NOT EXISTS `nl_stores` (
cultureid INT(10) NOT NULL,
name VARCHAR(150) NOT NULL
) DEFAULT CHARSET=utf8;
Meaning you have the cultureid and name fields in it, then you can use this query to find the results you are looking for, using the HAVING method:
SELECT cultureid, name FROM nl_stores GROUP BY name HAVING(COUNT(cultureid) = 1);
Another way to do it would be using the EXISTS statmant:
SELECT nl_stores.cultureid, nl_stores.name FROM nl_stores WHERE !EXISTS(SELECT 1 FROM nl_stores AS nl_stores_dummy WHERE nl_stores_dummy.name = nl_stores.name AND nl_stores_dummy.cultureid != nl_stores.cultureid);
And of course you can also achieve it using a JOIN:
SELECT nl_stores.cultureid, nl_stores.name FROM nl_stores LEFT JOIN nl_stores AS nl_stores_dummy ON nl_stores_dummy.name = nl_stores.name WHERE nl_stores_dummy.cultureid IS NULL;
Here is a full example based on the data you provided I consider the following DB structure:
CREATE TABLE IF NOT EXISTS `nl_cultures` (
cultureid INT(10) NOT NULL,
culture_name VARCHAR(150) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `nl_cultures` (`cultureid`, `culture_name`) VALUES
('4', 'Culture 1'),
('5', 'Culture 2');
CREATE TABLE IF NOT EXISTS `nl_stores` (
cultureid INT(10) NOT NULL,
name VARCHAR(150) NOT NULL,
`status` TINYINT(1) NOT NULL
) DEFAULT CHARSET=utf8;
And the Query could be:
SELECT nl_cultures.culture_name, nl_stores.name AS store_name
FROM nl_cultures
INNER JOIN nl_stores ON nl_stores.cultureid = nl_cultures.cultureid
WHERE nl_stores.status = 1
GROUP BY nl_stores.name
HAVING(COUNT(nl_stores.cultureid) = 1);
Usually the JOIN statement would be the fastest executed, but it depends on the amount of results you have in the table, based on that I would consider using the GROUP, which should be fast enough and works without any additional joins.
A couple of tips to consider which I learned in my experience:
It would be best if you do not compare names, but IDs of those stores, it would be much, much, much faster also a lot better for indexing. If it is not the ID of the store, cause they are different in your different cultures, then consider providing them with some numeric key which identifies them and is the same no matter the culture.
Also It is a lot easier to understand and follow up a query when you set understandable/readable table names and use them as references. For example: store AS store_joined instead of store AS s, because s is nothing and when you have a really long query with a couple of joins it could become difficult to distinguish all of them and really easy to skip any. Same goes for the column names, also it is better to separate column words with under underscore, cultureid, should be culture_id.
One more is to consider not using special keywords like status as column names. If you are using some kind of DB tool it will be easier to track that.
And one last thing, based on the query you have typed in the screenshot, use UPPER case letters for the sql keywords, easier to review the structure.
A nice tool to test your sql: http://sqlfiddle.com.
Hope I was helpful and if you have any question, please let me know and vote my answer if it worked for you :).

Joining three tables in SQLite

I am writing an SQL query in order to fetch data, which is to be populated in the scrollable table .
Content id's of the contents which are going to be compared(Content1, Content2, Content3 .. in the image above) will be the input to the query.
Since, for comparison minimum 2 items are required, the number of id's passed to the query will always be 2 or greater than 2.
Following is the SQL of 3 tables, from which the required data is to be fetched.
Following table contains the Paramter names that are to be compared:
CREATE TABLE tbl_content_commons (
id integer PRIMARY KEY,
content_common_code char(20) NOT NULL,
content_common_name char(100) NOT NULL // The comparison label
)
Following table contains the code of comparison label(content_common_name char) in the table above and the Content id of the contents(which would be passed as the parameter for the query)
CREATE TABLE tbl_comparison_values (
id integer PRIMARY KEY,
tbl_content_common_id integer NOT NULL,// ID's of the Contents under comparison
userneed_comparison_label_id integer NOT NULL,// ID of comparison label in the table above
value char(50) NOT NULL// Value corresponding to a comparison label - if it exists for a given content id
)
Finally, the table containing the name of the contents(Content1, Content2..) whose id's were passed as parameters to the query
CREATE TABLE userneed_comparison_labels (
id integer PRIMARY KEY,
name char(50) NOT NULL// Name of the content whose id's are passed through queries. content ID in the table above
)
I have made enough of efforts in writing a query to fetch data that would help me populate the table shown in the attached image, but did not succeed. I can show the queries I have written, but since it again prolongs the question, I am not posting here.
Any guidance or help as to how to proceed, writing this SQL query would be much appreciated.
How about this...
select c.content_common_name,
l.name,
v.value
from userneed_comparison_labels l
left join tbl_comparison_values v on l.id = v.userneed_comparison_label_id
left join tbl_content_commons c on c.id = v.tbl_content_common_id
where c.id in (1, 2, 3)
See SQL Fiddle for more detail.
Choose SQLLite (SQL.js) version. Click cancel if it asks if you want to use WebSQL.