15

When I ask a question, generally it is programming related. I simply go to whatever code I am working on, make an example mockup of the code, exclude specific variables and identifying information to the specific application I am working on, and then I post the mockup code. Generally my questions are answered right away, usually without hiccup.

However when it comes to MySQL related issues, I find it much harder if not impossible to get answers. I do the same general idea of looking at the problem I am having, and creating mock data to post when asking my question. However I find when it comes to MySQL IT SEEMS so far based on my limited experiences on this site, that people are not so accepting to work with mock data.

In one such recent question it has so far ended in a string of comments where I expressed confusion about what was being asked of me when it comes to "sample data", and it was told to me that "I need to run actual queries on the database I am working on and post the results of those queries".

Obviously, I am not going to do that as I am dealing with a live database, and I am not about to just go sharing data from it on Stack Overflow.

I suppose my question, without trying to start any kind of huge debate, is: How exactly do I generate a MySQL question without posting actual contents of my database that will be acceptable by others to actually answer?

Here is an example of what I mean: https://stackoverflow.com/questions/39285286/complex-joins-and-unions-mysql?noredirect=1#comment65912520_39285286

As can be seen on that post, I shared mockup database data. I could literally write a CREATE database and a series of insert commands that would recreate the example mockup I gave, and reproduce my problem. So what is it about ACTUAL data that is so important? How do I fix the MySQL questions I ask so people actually answer instead of demand me expose the database I am working on?

10
  • 13
    "I could literally write a CREATE database and a series of insert commands that would recreate the example mockup I gave, and reproduce my problem." So do that then. You've provided a query and the result of that query but not the source data that led to those results. You should also provide the expected results. With the source data and expected results provided it is easy for answerers to validate that their answers actually produce what you want. Commented Sep 4, 2016 at 7:33
  • This is the type of terminology that frustrates me. "You've provided a query and the result of that query but not the source data." By definition source data is the location that data comes from. ITS MOCKUP DATA. There is no primary location except my head from whence it came. I could write a mockup database creation query but, mix with that people tend to copy and paste mysql code into sqlfiddle and then you have to spend time explaining why using sqlfiddle is like plugging mysql code into mssql.... and it doesn't always produce the same results nor problems, and it becomes a complicated mess
    – Bruce
    Commented Sep 4, 2016 at 9:53
  • 3
    I don't get what's so difficult about it. No one's asking you to provide actual data - that's probably illegal! All we want is a properly representative data set (formulated as a set of CREATE and INSERT statements) together with a desired result. Hundreds of questions are asked here everyday. Is it not reasonable that those asking the questions undertake some of the basic legwork necessary to simplify the process of answering them?
    – Strawberry
    Commented Sep 4, 2016 at 10:45
  • 5
    Please see How to create a Minimal, Complete, and Verifiable example plenty of other people manage it in the SQL tags. There's nothing inherent about SQL that prevents it. You should include DDL with minimal example data and a minimal query that reproduces the issue you are asking about and desired results for that example data. You can feel free to ignore this advice but as your question has sat there for 2 days unanswered your current approach doesn't seem to be working out for you. Commented Sep 4, 2016 at 11:42
  • 3
    @Strawberry the only thing difficult about it is I am thick and don't often understand what is actually being asked of me. You made it very clear as have a few other comments that, I need to include the select and insert statements to actually create the database. Now that I understand that, I will always do that, however I have a feeling I will be explaining to people rather frequently why sqlfiddle won't recreate the problems as generally when I have sql problems they are not simple statements.
    – Bruce
    Commented Sep 4, 2016 at 22:02
  • @MartinSmith obviously I already understand that link or the entire first paragraph of this question wouldn't make an awful lot of sense. I am understanding now however, I need to include the database creation queries, the queries being run against the tables, the example results of those queries, and of course the expected outcome.
    – Bruce
    Commented Sep 4, 2016 at 22:05
  • Sounds like the penny's dropped
    – Strawberry
    Commented Sep 4, 2016 at 22:09
  • @Strawberry it has indeed, unfortunately not before I started losing what little rep points I have lol. But hey, live and learn right :)
    – Bruce
    Commented Sep 4, 2016 at 22:19
  • 1
    @Bruce I wrote this silly Q and self-answer entitled What is Sqlfiddle and why should I care? but in fact it is pretty serious. At least as serious as you guys want answers to your db questions. It takes us 10 minutes or more sometimes to create the tables and load in test data. We should be able to get all edge condition data from the OP and not have to do this. We don't need actual data. We need actual table schemas and fake data with boundary conditions. Otherwise we just skip the question and ignore it. Also, actual expected results, please.
    – Drew
    Commented Sep 5, 2016 at 19:47
  • I have never written an INSERT statement before my SQL questions, but I do describe the hypothetical tables and type out some sample entries.
    – SandPiper
    Commented Dec 14, 2017 at 12:13

2 Answers 2

21

Use a find, and replace any specific names with generic names in an IDE like Notepad++. It can be painful, as I've asked many questions that don't make sense slipping up with that and then having to edit it. But it's the only way to anonymise any code.

You should never have to share any real data. If people hassle you for that, bring it to meta with a question.

|   id   |   username   |   data    |   data2   |   id   |   data3   |    id   |   data4   |
--------------------------------------------------------------------------------------------
|    1   |   someuser   | some data | more data |    2   | more data |     3   | more data |
|    1   |   someuser   | some data | more data |    3   | more data |     2   | more data |
|    2   |   someuser   | some data | more data |    3   | more data |     1   | more data |
|    2   |   someuser   | some data | more data |    1   | more data |     3   | more data |

Replace the repeated values with unique values e.g. user1, some data1, more data1.. and so on.
Perhaps consider using name, address, city, instead of data2, data3. Having mock names Fred, Harry, Sally, and City, Sydney, NY, and so on. It's easier to read when there's a greater difference between these names.

|   id   |   username   |   name   |   city   |   id   |   street   |    id   |   Pets  |
--------------------------------------------------------------------------------------------
|    1   |   fred101  | Fred        | NY      |    2   | Apple Cove |     3   | Rover   |
|    1   |   hazza    | Harry       | Sydney  |    3   | Sodds St   |     2   | Rocky   |
|    2   |   sally7   | Sally       | London  |    3   | Rigor Road |     1   | Sid     |
|    2   |   bendo    | Ben         | Hobart  |    1   | Moon Cres  |     3   | Boots   |

Now this dummy data may not be consistent with what you're trying to ask and how the data matches, but this is where there is a lack of clarity in what you are asking.

Also with the code:

SELECT * from table1
LEFT OUTER JOIN table2
ON table1.username=table2.username
UNION
SELECT * from table2
LEFT OUTER JOIN table1
ON table2.username=table1.username
WHERE table1.username= :username
AND table2.username = :username

This is difficult to process also, as the repeated use of a username with the generic table appended with a number makes the mental gymnastics in the brain harder.

If you look at something like this, it's easier to follow:

SELECT * from Users
LEFT OUTER JOIN Members
ON Users.username=Members.username
UNION
SELECT * from Members
LEFT OUTER JOIN Users
ON Members.username=Users.username
WHERE Users.username= :username
AND Members.username = :username

Obviously match the data table with the query.

9
  • I was actually contemplating if I should perhaps create a local database with a bunch of fake users and then import other tables as needed based on the projects I am working on so I can easily create mock data, I do concern myself with security ESPECIALLY when using mysql + php, there should be no "pain" nor any risk of "forgetting to remove" something if all the user data is mocked on a fake database. Based on what you are saying I think it may be the route to go.
    – Bruce
    Commented Sep 3, 2016 at 6:24
  • I have done exactly as suggested, the ironic part, is the problem I am having relates to duplicate data so its really easy to do.
    – Bruce
    Commented Sep 3, 2016 at 6:49
  • @Bruce ok, so the only confusion I have now with your SO question, is it appears all that data is duplicated.
    – user3956566
    Commented Sep 3, 2016 at 6:52
  • YES! that is the problem!!!
    – Bruce
    Commented Sep 3, 2016 at 6:52
  • @Bruce then you've nailed it ;) p.s. it took ages putting in the dummy data lol
    – user3956566
    Commented Sep 3, 2016 at 6:53
  • @Bruce maybe show the dummy data as it sits in the tables
    – user3956566
    Commented Sep 3, 2016 at 6:54
  • 2
    I can't thank you enough for your help :)
    – Bruce
    Commented Sep 3, 2016 at 6:58
  • 3
    @Bruce not a problem, it's meta working! ;)
    – user3956566
    Commented Sep 3, 2016 at 7:04
  • 1
    @Yvette For generating those sorts of table, look at ozh.github.io/ascii-tables :).
    – Tunaki
    Commented Sep 5, 2016 at 19:29
4

When asking this type of SQL question, always provide all of the CREATE TABLE, INSERT statements, etc. that are needed to reproduce the problem. It doesn't have to be your real data and tables, just ones that reproduce your problem. To do otherwise is disrespectful to the people who try to answer your questions.

Why? Because you only have to do it once, but if you don't do it, every person who attempts to answer your question has to do it. In addition, you know what the problem is and how the tables and data should look to reproduce the problem, but others will have to redo work you've already done to get there.

For SQL, these things are the MCVE, and not posting one is like saying "my time is much more important than yours". After all, you're choosing not to spend a few minutes on it, which means potential answerers are collectively wasting potentially hours in total for the same thing.

If it'd be up to me, when you select the SQL tag for a question, you'd get a popup with big red letters prompting you to remember to include an MCVE if applicable, and detailing that that includes any CREATE TABLE and INSERT statements needed to create the proper environment as well as the actual problematic queries against the data, the results they give, and the results you wanted.

So as Martin Smith already eloquently put it in the comments:

I could literally write a CREATE database and a series of insert commands that would recreate the example mockup I gave, and reproduce my problem.

So do that then.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .